From 27dad3d9d6c70198fbd4e46b07adc462189885b1 Mon Sep 17 00:00:00 2001 From: Florian Oltmer Date: Wed, 4 Jul 2018 23:05:13 +0200 Subject: [PATCH 1/4] Added replset Status Command --- lib/src/database/db.dart | 5 +++++ lib/src/database/dbcommand.dart | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/lib/src/database/db.dart b/lib/src/database/db.dart index 7737851c..2c7d2da3 100644 --- a/lib/src/database/db.dart +++ b/lib/src/database/db.dart @@ -334,6 +334,11 @@ class Db { connection: connection); } + Future> getStatus({_Connection connection}) { + return executeDbCommand(DbCommand.createStatusCommand(this), + connection: connection); + } + Future> isMaster({_Connection connection}) { return executeDbCommand(DbCommand.createIsMasterCommand(this), connection: connection); diff --git a/lib/src/database/dbcommand.dart b/lib/src/database/dbcommand.dart index 85ce7b7f..0b0aa44f 100644 --- a/lib/src/database/dbcommand.dart +++ b/lib/src/database/dbcommand.dart @@ -112,6 +112,10 @@ class DbCommand extends MongoQueryMessage { return createQueryDbCommand(db, {'buildInfo': 1}); } + static DbCommand createStatusCommand(Db db) { + return createQueryDbCommand(db, {'replSetGetStatus': 1 }); + } + static DbCommand createGetLastErrorCommand(Db db, WriteConcern concern) { return createQueryDbCommand(db, concern.command); } From 3407ec83ae4350f170054eee99fd7f6fc6ecaf1f Mon Sep 17 00:00:00 2001 From: Florian Oltmer Date: Thu, 5 Jul 2018 12:50:42 +0200 Subject: [PATCH 2/4] Added getStatus example --- example/get_replica_status.dart | 46 +++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 example/get_replica_status.dart diff --git a/example/get_replica_status.dart b/example/get_replica_status.dart new file mode 100644 index 00000000..29e41206 --- /dev/null +++ b/example/get_replica_status.dart @@ -0,0 +1,46 @@ +import 'package:mongo_dart/mongo_dart.dart'; + +/// At the moment you can only run getStatus() command on primary +/// On secondary we get a "MongoDart Error: No master connection" because we have no read permission on it +/// To test with secondary you can comment our the lines 29 and 31 in connection_manager.dart +main() async { + var db = new Db("mongodb://127.0.0.1:27017/admin"); + await db.open(); + + Map rep_status = await db.getStatus(); + //print(rep_status); + if (rep_status.containsKey("members")) { + List members = rep_status['members']; + int members_count = members.length; + print("Members Count: " + members_count.toString()); + members.forEach((dynamic value) { + print("-----------------------------------"); + //print(value); + int _id = value['_id']; + String name = value['name']; + double health = value['health']; + String stateStr = value['stateStr']; + int uptime = value['uptime']; + + print("Member id: " + _id.toString()); + print("Member name: " + name); + print("Member health: " + health.toString()); + print("Member State: " + stateStr.toString()); + print("Member State: " + secToTime(uptime)); + }); + } + + await db.close(); +} + +String secToTime(int seconds) { + double hours = seconds / 3600; + double mins = seconds / 60 % 60; + int secs = seconds % 60; + return hours.floor().toString() + + "h " + + mins.floor().toString() + + "m " + + secs.floor().toString() + + "s"; +} From 0a7851615254cb273502850bd33db9acc04bf2e5 Mon Sep 17 00:00:00 2001 From: Florian Oltmer Date: Thu, 5 Jul 2018 14:38:03 +0200 Subject: [PATCH 3/4] Added getStatus test --- test/database_test.dart | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/database_test.dart b/test/database_test.dart index 72e74312..af42de74 100644 --- a/test/database_test.dart +++ b/test/database_test.dart @@ -9,6 +9,7 @@ import 'package:uuid/uuid.dart'; const dbName = "test-mongo-dart"; const DefaultUri = 'mongodb://localhost:27017/$dbName'; +const DefaultAdminUri = 'mongodb://localhost:27017/admin'; Db db; Uuid uuid = new Uuid(); @@ -65,6 +66,11 @@ Future getBuildInfo() async { expect(result["ok"], 1); } +Future getStatus() async { + var result = await db.getStatus(); + expect(result["ok"], 1); +} + Future testIsMaster() async { var result = await db.isMaster(); expect(result["ok"], 1); @@ -1142,6 +1148,11 @@ Future testFindOneWhileStateIsOpening() { } main() { + Future initializeAdminDatabase() async { + db = new Db(DefaultAdminUri); + await db.open(); + } + Future initializeDatabase() async { db = new Db(DefaultUri); await db.open(); @@ -1151,6 +1162,20 @@ main() { await db.close(); } + group("Admin", () { + setUp(() async { + await initializeAdminDatabase(); + }); + + tearDown(() async { + await cleanupDatabase(); + }); + + group('DBCommand:', () { + test('getStatus', getStatus); + }); + }); + group("A", () { setUp(() async { await initializeDatabase(); From 0ad8121e248914c1c22e20bbcdbdd369d9eddbad Mon Sep 17 00:00:00 2001 From: Florian Oltmer Date: Sat, 6 Oct 2018 13:37:21 +0200 Subject: [PATCH 4/4] Changed: permanent masterConnection for getStatus on secondary Server --- lib/src/network/connection_manager.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/network/connection_manager.dart b/lib/src/network/connection_manager.dart index 10f60bb5..981a914b 100644 --- a/lib/src/network/connection_manager.dart +++ b/lib/src/network/connection_manager.dart @@ -26,9 +26,9 @@ class _ConnectionManager { _log.fine(() => replyMessage.documents[0].toString()); var master = replyMessage.documents[0]["ismaster"] == true; connection.isMaster = master; - if (master) { + //if (master) { _masterConnection = connection; - } + //} connection.serverCapabilities .getParamsFromIstMaster(replyMessage.documents[0]); if (db._authenticationScheme == null) {