@Test public void testPreSplit() { List<DBObject> shards = new ArrayList<DBObject>(); shards.add(new BasicDBObject("_id", "shard0")); shards.add(new BasicDBObject("_id", "shard1")); BasicDBList listShards = new BasicDBList(); listShards.add(new BasicDBObject("shards", shards)); List<String> lShards = new ArrayList<String>(); lShards.add("shard0"); lShards.add("lShard1"); Mockito.when(db.command((DBObject) Matchers.any())).thenReturn(res); Mockito.when(res.get("shards")).thenReturn(listShards); String result = MongoCommander.preSplit(shardCollections, dbName, mockedMongoTemplate); assertNull(result); Mockito.verify(db, Mockito.times(1)).command(new BasicDBObject("enableSharding", dbName)); Mockito.verify(db, Mockito.times(2)).command(new BasicDBObject("listShards", 1)); Mockito.verify(db, Mockito.times(13)).command(Matchers.any(DBObject.class)); Mockito.verify(settings, Mockito.times(1)).update(Matchers.any(DBObject.class), Matchers.any(DBObject.class), Matchers.eq(true), Matchers.eq(false)); }
public static String preSplit(Set<String> shardCollections, String dbName, MongoTemplate mongoTemplate) { DB dbConn = mongoTemplate.getDb().getSisterDB("admin"); List<String> shards = getShards(dbConn); if (shards.size() == 0) { return null; } String sresult = setBalancerState(dbConn, false); if (sresult != null) { return sresult; } DBObject enableShard = new BasicDBObject("enableSharding", dbName); CommandResult result = dbConn.command(enableShard); if (!result.ok()) { LOG.error("Error enabling sharding on {}: {}", dbConn, result.getErrorMessage()); return result.getErrorMessage(); } for (String coll : shardCollections) { String collection = dbName + "." + coll; DBObject shardColl = new BasicDBObject(); shardColl.put("shardCollection", collection); shardColl.put("key", new BasicDBObject(ID, 1)); result = dbConn.command(shardColl); if (!result.ok()) { LOG.error("Error enabling shard'ing on {}: {}", collection, result.getErrorMessage()); return result.getErrorMessage(); } sresult = moveChunks(collection, shards, dbConn); if (sresult != null) { return sresult; } } return preSplitBinCollections(dbName, mongoTemplate); }
MongoCommander { public static String preSplit(Set<String> shardCollections, String dbName, MongoTemplate mongoTemplate) { DB dbConn = mongoTemplate.getDb().getSisterDB("admin"); List<String> shards = getShards(dbConn); if (shards.size() == 0) { return null; } String sresult = setBalancerState(dbConn, false); if (sresult != null) { return sresult; } DBObject enableShard = new BasicDBObject("enableSharding", dbName); CommandResult result = dbConn.command(enableShard); if (!result.ok()) { LOG.error("Error enabling sharding on {}: {}", dbConn, result.getErrorMessage()); return result.getErrorMessage(); } for (String coll : shardCollections) { String collection = dbName + "." + coll; DBObject shardColl = new BasicDBObject(); shardColl.put("shardCollection", collection); shardColl.put("key", new BasicDBObject(ID, 1)); result = dbConn.command(shardColl); if (!result.ok()) { LOG.error("Error enabling shard'ing on {}: {}", collection, result.getErrorMessage()); return result.getErrorMessage(); } sresult = moveChunks(collection, shards, dbConn); if (sresult != null) { return sresult; } } return preSplitBinCollections(dbName, mongoTemplate); } }
MongoCommander { public static String preSplit(Set<String> shardCollections, String dbName, MongoTemplate mongoTemplate) { DB dbConn = mongoTemplate.getDb().getSisterDB("admin"); List<String> shards = getShards(dbConn); if (shards.size() == 0) { return null; } String sresult = setBalancerState(dbConn, false); if (sresult != null) { return sresult; } DBObject enableShard = new BasicDBObject("enableSharding", dbName); CommandResult result = dbConn.command(enableShard); if (!result.ok()) { LOG.error("Error enabling sharding on {}: {}", dbConn, result.getErrorMessage()); return result.getErrorMessage(); } for (String coll : shardCollections) { String collection = dbName + "." + coll; DBObject shardColl = new BasicDBObject(); shardColl.put("shardCollection", collection); shardColl.put("key", new BasicDBObject(ID, 1)); result = dbConn.command(shardColl); if (!result.ok()) { LOG.error("Error enabling shard'ing on {}: {}", collection, result.getErrorMessage()); return result.getErrorMessage(); } sresult = moveChunks(collection, shards, dbConn); if (sresult != null) { return sresult; } } return preSplitBinCollections(dbName, mongoTemplate); } private  MongoCommander(); }
MongoCommander { public static String preSplit(Set<String> shardCollections, String dbName, MongoTemplate mongoTemplate) { DB dbConn = mongoTemplate.getDb().getSisterDB("admin"); List<String> shards = getShards(dbConn); if (shards.size() == 0) { return null; } String sresult = setBalancerState(dbConn, false); if (sresult != null) { return sresult; } DBObject enableShard = new BasicDBObject("enableSharding", dbName); CommandResult result = dbConn.command(enableShard); if (!result.ok()) { LOG.error("Error enabling sharding on {}: {}", dbConn, result.getErrorMessage()); return result.getErrorMessage(); } for (String coll : shardCollections) { String collection = dbName + "." + coll; DBObject shardColl = new BasicDBObject(); shardColl.put("shardCollection", collection); shardColl.put("key", new BasicDBObject(ID, 1)); result = dbConn.command(shardColl); if (!result.ok()) { LOG.error("Error enabling shard'ing on {}: {}", collection, result.getErrorMessage()); return result.getErrorMessage(); } sresult = moveChunks(collection, shards, dbConn); if (sresult != null) { return sresult; } } return preSplitBinCollections(dbName, mongoTemplate); } private  MongoCommander(); static String ensureIndexes(String indexFile, String db, MongoTemplate mongoTemplate); static String ensureIndexes(Set<String> indexes, String db, MongoTemplate mongoTemplate); static String ensureIndexes(Set<MongoIndex> indexes, DB dbConn); @SuppressWarnings("boxing") static String ensureIndexes(MongoIndex index, DB dbConn, int indexOrder); static String preSplit(Set<String> shardCollections, String dbName, MongoTemplate mongoTemplate); static DB getDB(String db, MongoTemplate mongoTemplate); static DB getDB(String db, DB dbConn); }
MongoCommander { public static String preSplit(Set<String> shardCollections, String dbName, MongoTemplate mongoTemplate) { DB dbConn = mongoTemplate.getDb().getSisterDB("admin"); List<String> shards = getShards(dbConn); if (shards.size() == 0) { return null; } String sresult = setBalancerState(dbConn, false); if (sresult != null) { return sresult; } DBObject enableShard = new BasicDBObject("enableSharding", dbName); CommandResult result = dbConn.command(enableShard); if (!result.ok()) { LOG.error("Error enabling sharding on {}: {}", dbConn, result.getErrorMessage()); return result.getErrorMessage(); } for (String coll : shardCollections) { String collection = dbName + "." + coll; DBObject shardColl = new BasicDBObject(); shardColl.put("shardCollection", collection); shardColl.put("key", new BasicDBObject(ID, 1)); result = dbConn.command(shardColl); if (!result.ok()) { LOG.error("Error enabling shard'ing on {}: {}", collection, result.getErrorMessage()); return result.getErrorMessage(); } sresult = moveChunks(collection, shards, dbConn); if (sresult != null) { return sresult; } } return preSplitBinCollections(dbName, mongoTemplate); } private  MongoCommander(); static String ensureIndexes(String indexFile, String db, MongoTemplate mongoTemplate); static String ensureIndexes(Set<String> indexes, String db, MongoTemplate mongoTemplate); static String ensureIndexes(Set<MongoIndex> indexes, DB dbConn); @SuppressWarnings("boxing") static String ensureIndexes(MongoIndex index, DB dbConn, int indexOrder); static String preSplit(Set<String> shardCollections, String dbName, MongoTemplate mongoTemplate); static DB getDB(String db, MongoTemplate mongoTemplate); static DB getDB(String db, DB dbConn); static final String STR_0X38; }