0

I installed 4.4 version MongoDB and reproduced replica set, sharding for my Java application.

Now, I am looking for the approach of run MongoDB shell command in my Java application for check and monitoring reproduced MongoDB Cluster's replica set and sharding status. I'm curious how to achieve this.

I tried to find answers. but, I couldn't found clear answer from other stackoverflow question regarding MongoDB. because most questions discuss base on 3.x version.

So, I researched MongoDB document and driver for 4.4 version and Tested that like below.

  1. run MongoDB command
    • I tried to run "sh.status()" and "rs.status()" command using MongoDB 4.5 driver through MongoClient object and runCommand() method. but, Java return error message.
    • I realized that runCommand() method only allow "Mongo" commands mentioned on below link and not Mongo Shell Command.
      link : https://www.mongodb.com/docs/v4.4/reference/command/
    • I couldn't found contrasting command with sh.status() from above link.
String uriString = "mongodb://"+userName+":"+passWord+"@"+targetHost+":"+portNo;
uriString = uriString +"/?authSource="+tgDBName+"&authMechanism="+authMechanism;

MongoClient mongoClient = MongoClients.create(uriString);

String commandString    = "sh.status()";
Bson command            = new BsonDocument(commandString, new BsonInt64(1));
Document commandResult  = database.runCommand(command);
com.mongodb.MongoCommandException: Command failed with error 59 (CommandNotFound):   
'no such cmd: sh.status()' on server ....
  1. run javascript stored in MongoDB

Finally, I considering access to MongoDB using JSch and run prompt.
But, I think that is not formal approach. so, I want know other way for get metrics
like "run MongoDB Shell Command in java using 4.4 version driver"

Note : when run sh.status() command in mongos, I received metrics successfully like below.

mongos> sh.status()
    --- Sharding Status ---
      sharding version: {
            "_id" : 1,
            "minCompatibleVersion" : 5,
            "currentVersion" : 6,
            "clusterId" : ObjectId("625513e0838da178377f6900")
      }
      shards:
            {  "_id" : "sh01",  "host" : "sh01/WIN-BKEV4AO0KED:27011,WIN-BKEV4AO0KED:27012,WIN-BKEV4AO0KED:27013",  "state" : 1 }
            {  "_id" : "sh02",  "host" : "sh02/WIN-BKEV4AO0KED:27021,WIN-BKEV4AO0KED:27022,WIN-BKEV4AO0KED:27023",  "state" : 1 }
      active mongoses:
            "4.4.13" : 1
      autosplit:
            Currently enabled: yes
      balancer:
            Currently enabled:  yes
            Currently running:  no
            Failed balancer rounds in last 5 attempts:  0
            Migration Results for the last 24 hours:
                    No recent migrations
      databases:
            {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
leeyounsoo
  • 19
  • 4
  • are you looking for this? "db.printShardingStatus()" https://www.mongodb.com/docs/manual/reference/method/db.printShardingStatus/#mongodb-method-db.printShardingStatus – Morph21 Apr 15 '22 at 10:28
  • Yes, I want result of "printShardingStatus()" or "sh.status()" function in Java Application. But, when I write that command/function on Java Application, Java occurred error like "no such cmd:" ........ – leeyounsoo Apr 18 '22 at 06:33

1 Answers1

0

rs.status() is a mongo shell helper for db.adminCommand({ replSetGetStatus: 1 })

I don't work with Java, but I guess you must use MongoDatabase.runCommand. Could be similar to this:

MongoDatabase database = mongoClient.getDatabase("admin");
String commandString    = "replSetGetStatus";
Bson command            = new BsonDocument(commandString, new BsonInt64(1));
Document commandResult  = database.runCommand(command);

For sh.status() or db.printShardingStatus() it is a bit more difficult. Documentation says:

The db.printShardingStatus() method run in mongosh does not return JSON. Use db.printShardingStatus() for manual inspection, and Config Database in scripts.

printShardingStatus

function printShardingStatus(configDB, verbose) {
    // configDB is a DB object that contains the sharding metadata of interest.
    // Defaults to the db named "config" on the current connection.
    if (configDB === undefined)
        configDB = db.getSiblingDB('config');

    var version = configDB.getCollection("version").findOne();
    if (version == null) {
        print(
            "printShardingStatus: this db does not have sharding enabled. be sure you are connecting to a mongos from the shell and not to a mongod.");
        return;
    }

    var raw = "";
    var output = function(indent, s) {
        raw += sh._shardingStatusStr(indent, s);
    };
    output(0, "--- Sharding Status --- ");
    output(1, "sharding version: " + tojson(configDB.getCollection("version").findOne()));
    ... many more lines

If you really like to get identical output to db.printShardingStatus(), then you would need to convert the content from that function into Java.

Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110
  • Thank you for the reply. I tried that logic on Java Application like "database.adminCommand( { replSetGetStatus : 1} )". but, db object looks has not adminCommand() method. That are settings on mongos? – leeyounsoo Apr 18 '22 at 06:35
  • Command "replSetGetStatus" needs to be executed on `mongod` only. Maybe let us know which information you are looking for. – Wernfried Domscheit Apr 18 '22 at 07:11
  • I looking for execute shell command like "sh.status()" in java application logic directly. not execute on "mongos", "mongod" instance. – leeyounsoo Apr 19 '22 at 01:14
  • @leeyounsoo Please see my update – Wernfried Domscheit Apr 19 '22 at 06:17
  • Thank you for update. Convert to Java is not expected to be easy. So, I decide to execute MongoDB command directly and get metrics using JSch package. and I try to write sample source code for figure out. – leeyounsoo Apr 20 '22 at 05:58