Example usage for com.mongodb CommandResult getBoolean

List of usage examples for com.mongodb CommandResult getBoolean

Introduction

In this page you can find the example usage for com.mongodb CommandResult getBoolean.

Prototype

public boolean getBoolean(final String key) 

Source Link

Document

Returns the value of a field as a boolean.

Usage

From source file:com.ca.apm.mongo.Collector.java

License:Open Source License

public Topology discoverTopology() throws Exception {

    final String host = getStringProp(DB_HOST_PROP);
    final int port = getIntProp(DB_PORT_PROP);

    logger.log(Level.INFO, "Discovering Topology for host: {0}:{1}", new Object[] { host, port });

    final CommandResult master = dbAdminCmd(host, port, "isMaster");

    // ismaster returns true for a standalone mongod instance, a mongos
    // instance, a mongod shard node, or a primary in a replica set
    if (master.getBoolean("ismaster") || master.containsField("primary")) {

        boolean isReplicaSet = false;
        if (master.containsField("primary")) {
            isReplicaSet = true;/* w  w  w. j av a  2  s.c  o  m*/
        }

        if (isInShardCluster(master)) {
            topology = new ShardCluster(props, host, port, logger);
            topology.discoverServers(getClusterNodeType());
        } else if (isReplicaSet(master)) {
            topology = new ReplicaSet(props, host, port, logger);
            topology.discoverServers("doesn't matter");
        } else {
            topology = new StandaloneMongod(props, host, port, logger);
            topology.discoverServers("doesn't matter");
        }
    }
    logger.log(Level.INFO, "Topology: {0}", topology);
    return topology;
}

From source file:com.ca.apm.mongo.Collector.java

License:Open Source License

private boolean isInShardCluster(final CommandResult cr) throws Exception {

    MongoServer ms = new MongoServer(getMyself(cr));
    final String host = ms.getHost();
    final int port = ms.getPort();

    boolean sharded = false;

    final String msg = cr.getString("msg");
    if ((msg != null) && msg.contains("isdbgrid")) {
        sharded = true;/*w  ww  .  ja va 2  s . com*/
    } else if (cr.getBoolean("ismaster")) {
        final CommandResult shardState = dbAdminCmd(host, port, "shardingState");
        // shardingState command only returns OK when server is in a sharded
        // cluster
        if (shardState.ok()) {
            if (shardState.getBoolean("enabled")) {
                sharded = true;
            }
        }
    } else if (cr.containsField("primary")) {
        // we are in a replica set but not the primary,
        // check the primary to see if it is a shard member
        final String primary = cr.getString("primary");
        ms = new MongoServer(primary);
        final CommandResult priIsMaster = dbAdminCmd(ms.getHost(), ms.getPort(), "isMaster");
        sharded = isInShardCluster(priIsMaster);
    }
    return sharded;
}

From source file:com.ca.apm.mongo.Collector.java

License:Open Source License

private String getClusterNodeType() throws Exception {

    final String host = getStringProp(DB_HOST_PROP);
    final int port = getIntProp(DB_PORT_PROP);

    String nodeType = null;//from w ww  .j  ava  2 s  .c o m

    final CommandResult isMaster = dbAdminCmd(host, port, "isMaster");
    if (isMaster.getBoolean("ismaster")) {
        final String msg = isMaster.getString("msg");
        if (msg != null && msg.contains("isdbgrid")) {
            nodeType = "shardRouter";
        } else {
            final CommandResult shardState = dbAdminCmd(host, port, "shardingState");
            if (shardState.ok() && shardState.getBoolean("enabled")) {
                if (isConfigServer(host, port)) {
                    nodeType = "shardConfigServer";
                } else {
                    nodeType = "shardMember";
                }
            }
        }
    } else if (isReplicaMember(isMaster)) {
        nodeType = "shardMember";
    }

    return nodeType;
}

From source file:com.ca.apm.mongo.Collector.java

License:Open Source License

/**
 * This method is to check to see if a node is in a replica set despite
 * not being the primary member./*from w  w  w  .  j  a  va 2  s.  c om*/
 */
private boolean isReplicaMember(final CommandResult cr) {
    boolean isReplMember = false;
    if (cr.containsField("primary")) {
        if (cr.getBoolean("secondary") || cr.getBoolean("passive") || cr.getBoolean("arbiterOnly")) {
            isReplMember = true;
        }
    }
    return isReplMember;
}

From source file:com.ca.apm.mongo.ShardCluster.java

License:Open Source License

private List<String> getConfigServersFromShard(final String host, final int port) throws Exception {

    final List<String> cfgServers = new ArrayList<String>();

    String cHost = host;/*from   www .j  a  v  a  2s.  co m*/
    int cPort = port;

    final CommandResult isMaster = dbAdminCmd(cHost, cPort, "isMaster");

    // we can't run the DB command "shardingState" from a node
    // which isn't a master.  This should only apply to non-primary
    // replica members, so find the primary and run the command on it.
    if (!isMaster.getBoolean("ismaster")) {
        if (isMaster.containsField("primary")) {
            final String primary = isMaster.getString("primary");
            final MongoServer ms = new MongoServer(primary);
            cHost = ms.getHost();
            cPort = ms.getPort();
        }
    }

    CommandResult shardState = dbAdminCmd(cHost, cPort, "shardingState");

    if (shardState.ok()) {
        final String cfgSrvs = shardState.getString("configServer");
        addCommaSeparatedHosts(cfgServers, cfgSrvs);
    }
    return cfgServers;
}

From source file:com.ca.apm.mongo.Topology.java

License:Open Source License

protected List<String> discoverReplicas(final String host, final int port) throws Exception {

    List<String> replicas = new ArrayList<String>();

    final CommandResult cr = dbAdminCmd(host, port, "isMaster");

    replicas.addAll((List<String>) cr.get("hosts"));
    // hidden replicas are replicas that cannot become primaries and
    // are hidden to the client app
    if (cr.getBoolean("hidden")) {
        // TODO: We can't assume we're the master here....
        replicas.add(cr.getString("me"));
    }/* ww  w.  jav a 2s .  co  m*/
    // passives are replicas that cannot become primaries because
    // their priority is set to 0
    if (cr.containsField("passives")) {
        replicas.addAll((List<String>) cr.get("passives"));
    }
    // arbiters exist only to vote in master elections.  They don't
    // actually hold replica data.
    if (cr.containsField("arbiters")) {
        replicas.addAll((List<String>) cr.get("arbiters"));
    }
    return replicas;
}

From source file:com.edgytech.umongo.ServerPanel.java

License:Apache License

@Override
protected void updateComponentCustom(JPanel comp) {
    try {/*from w  w w .  j av a 2 s .co m*/
        ServerNode node = getServerNode();
        if (node.isConfig) {
            ((Menu) getBoundUnit(Item.replica)).enabled = false;
        }

        MongoClient svrMongo = node.getServerMongoClient();
        ServerAddress addr = getServerNode().getServerAddress();
        if (addr != null) {
            setStringFieldValue(Item.host, addr.toString());
            setStringFieldValue(Item.address, addr.getSocketAddress().toString());
        }

        CommandResult res = svrMongo.getDB("local").command("isMaster");
        boolean master = res.getBoolean("ismaster");
        String replication = MongoUtils.makeInfoString("master", master, "secondary",
                res.getBoolean("secondary"), "passive", res.getBoolean("passive"));
        setStringFieldValue(Item.replication, replication);
        ((Text) getBoundUnit(Item.replication)).showIcon = master;

        setStringFieldValue(Item.maxObjectSize, String.valueOf(svrMongo.getMaxBsonObjectSize()));

        //            ((CmdField) getBoundUnit(Item.serverStatus)).updateFromCmd(svrMongo);
        //
        //            DBObject svrStatus = ((DocField) getBoundUnit(Item.serverStatus)).getDoc();
        //            boolean dur = svrStatus.containsField("dur");
        //            ((Text)getBoundUnit(Item.journaling)).setStringValue(dur ? "On" : "Off");
        //            ((Text)getBoundUnit(Item.journaling)).showIcon = dur;
    } catch (Exception e) {
        UMongo.instance.showError(this.getClass().getSimpleName() + " update", e);
    }
}

From source file:org.graylog2.system.stats.mongo.MongoProbe.java

License:Open Source License

private BuildInfo createBuildInfo() {
    final BuildInfo buildInfo;
    final CommandResult buildInfoResult = adminDb.command("buildInfo");
    if (buildInfoResult.ok()) {
        buildInfo = BuildInfo.create(buildInfoResult.getString("version"),
                buildInfoResult.getString("gitVersion"), buildInfoResult.getString("sysInfo"),
                buildInfoResult.getString("loaderFlags"), buildInfoResult.getString("compilerFlags"),
                buildInfoResult.getString("allocator"), (List<Integer>) buildInfoResult.get("versionArray"),
                buildInfoResult.getString("javascriptEngine"), buildInfoResult.getInt("bits"),
                buildInfoResult.getBoolean("debug"), buildInfoResult.getLong("maxBsonObjectSize")

        );//  w w w  . j a  v  a2  s .c o m
    } else {
        buildInfo = null;
    }

    return buildInfo;
}