/** * get list of the shards * @param dbConn * @return */ private static List<String> getShards(DB dbConn) { List<String> shards = new ArrayList<String>(); DBObject listShardsCmd = new BasicDBObject("listShards", 1); CommandResult res = dbConn.command(listShardsCmd); if (!res.ok()) { LOG.error("Error getting shards for {}: {}", dbConn, res.getErrorMessage()); } BasicDBList listShards = (BasicDBList) res.get("shards"); //Only get shards for sharding mongo if (listShards != null) { ListIterator<Object> iter = listShards.listIterator(); while (iter.hasNext()) { BasicDBObject shard = (BasicDBObject) iter.next(); shards.add(shard.getString(ID)); } } return shards; }
private int doBatchUpdate(DBCollection dbCollection, String collName, Collection<BatchUpdateOptions> options, boolean ordered) { DBObject command = new BasicDBObject(); command.put("update", collName); List<BasicDBObject> updateList = new ArrayList<BasicDBObject>(); for (BatchUpdateOptions option : options) { BasicDBObject update = new BasicDBObject(); update.put("q", option.getQuery().getQueryObject()); update.put("u", option.getUpdate().getUpdateObject()); update.put("upsert", option.isUpsert()); update.put("multi", option.isMulti()); updateList.add(update); } command.put("updates", updateList); command.put("ordered", ordered); CommandResult commandResult = dbCollection.getDB().command(command); return Integer.parseInt(commandResult.get("n").toString()); }
private void assertVersionConfiguration(String configuredVersion, String expectedVersion) { this.context = new AnnotationConfigApplicationContext(); int mongoPort = SocketUtils.findAvailableTcpPort(); EnvironmentTestUtils.addEnvironment(this.context, "spring.data.mongodb.port=" + mongoPort); if (configuredVersion != null) { EnvironmentTestUtils.addEnvironment(this.context, "spring.mongodb.embedded.version=" + configuredVersion); } this.context.register(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class, EmbeddedMongoAutoConfiguration.class); this.context.refresh(); MongoTemplate mongo = this.context.getBean(MongoTemplate.class); CommandResult buildInfo = mongo.executeCommand("{ buildInfo: 1 }"); assertThat(buildInfo.getString("version")).isEqualTo(expectedVersion); }
@Override public void contribute(Info.Builder builder) { InetAddress ip = null; Map<String, String> hostMap = new HashMap<>(); try { ip = InetAddress.getLocalHost(); } catch (UnknownHostException e) { e.printStackTrace(); } hostMap.put("ipAddress", ip.getHostAddress()); hostMap.put("hostname", ip.getHostName()); builder.withDetail("appHostInfo", hostMap); hostMap = new HashMap<>(); CommandResult commandResult = this.mongoTemplate.executeCommand("{ serverStatus: 1 }"); hostMap.put("hostname", commandResult.getString("host")); builder.withDetail("mongoDbHostInfo", hostMap); }
private void assertVersionConfiguration(String configuredVersion, String expectedVersion) { this.context = new AnnotationConfigApplicationContext(); int mongoPort = SocketUtils.findAvailableTcpPort(); EnvironmentTestUtils.addEnvironment(this.context, "spring.data.mongodb.port=" + mongoPort); if (configuredVersion != null) { EnvironmentTestUtils.addEnvironment(this.context, "spring.mongodb.embedded.version=" + configuredVersion); } this.context.register(MongoAutoConfiguration.class, MongoDataAutoConfiguration.class, EmbeddedMongoAutoConfiguration.class); this.context.refresh(); MongoTemplate mongo = this.context.getBean(MongoTemplate.class); CommandResult buildInfo = mongo.executeCommand("{ buildInfo: 1 }"); assertThat(buildInfo.getString("version"), equalTo(expectedVersion)); }
/** * backup the last collection and put '.bak' to the end of name<br /> * be aware that the sharded mongodb does not support renameCollection() * <p> * * @author chen.chen.9, 2013-8-26 * @return {@link BackupHistoryDataBean} */ protected BackupHistoryDataBean backupHistoryData() { long startTime = System.currentTimeMillis(); String originalName = getResponseClassSimpleName(); String backupEndName = getSysconfig().getString("Collector.BackupEndName"); DateFormat dateFormat = new SimpleDateFormat(".yyyy-MM-dd_HH:mm"); dropPreviousCollection(originalName, backupEndName, dateFormat, getDaoSupport().getMongoTemplate()); String toName = StringUtils.join(new String[] { originalName, backupEndName, dateFormat.format(new Date()) }); String dbCommand = StringUtils.replaceEach("{ renameCollection: \"{0}.{1}\", to: \"{0}.{2}\", dropTarget: true }", new String[] { "{0}", "{1}", "{2}" }, new String[] { getSysconfig().getString("Database.MongoDB.DatabaseName"), originalName, toName }); CommandResult commandResult = getAdminMongoTemplate().executeCommand(dbCommand); return new BackupHistoryDataBean(originalName, toName, System.currentTimeMillis() - startTime, commandResult.ok(), dbCommand); }
/** * * mongodb,解析 更新操作是否成功 * * 返回更新数据库的结果 * * 小于零:更新出现异常 等于零:成功执行了更新的SQL,但是没有影响到任何数据 大于零:成功执行了更新的SQL,影响到多条数据,条数就是返回值 * * @param result * @return */ @Override public int getUpdateResult(WriteResult result) { if (result == null) { return FAIL_CODE_ONE; } @SuppressWarnings("deprecation") CommandResult cr = result.getLastError(); if (cr == null) { return FAIL_CODE_TWO; } boolean error_flag = false; error_flag = cr.ok(); if (!error_flag) {// 获取上次操作结果是否有错误. return FAIL_CODE_THREE; } int affect_count = result.getN();// 操作影响的对象个数 if (affect_count < 0) { return FAIL_CODE_FOUR; } else { return affect_count; } }
@Override public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes, Object ret) throws Throwable { AbstractSpan activeSpan = ContextManager.activeSpan(); CommandResult cresult = null; if (ret instanceof WriteResult) { WriteResult wresult = (WriteResult)ret; cresult = wresult.getCachedLastError(); } else if (ret instanceof AggregationOutput) { AggregationOutput aresult = (AggregationOutput)ret; cresult = aresult.getCommandResult(); } if (null != cresult && !cresult.ok()) { activeSpan.log(cresult.getException()); } ContextManager.stopSpan(); return ret; }
public static void testMongoUserId(int max, DB db) { String collName = "testmongobjid"; DBCollection coll = db.getCollection(collName); //Setup a sharded collection BasicDBObject command = new BasicDBObject(); command.put("shardcollection", collName); DBObject key = new BasicDBObject(); key.put("_id", 1); command.put("key", key); command.put("unique", true); db.command(command); long startM = System.currentTimeMillis(); for ( int i=0; i<max; i++ ) { BasicDBObject obj = new BasicDBObject(); obj.put("test", "value-"+i); coll.save(obj); } long endM = System.currentTimeMillis(); System.out.println("Insert " + max + " mongo objectid. time: " + (endM-startM) + " benchmark()"); CommandResult result = db.getStats(); System.out.println(result); }
public void run() { logger.log(Level.INFO, "harvesting metrics..."); for (String mongoSrv : topology.getDiscoveredServers()) { MongoServer ms = null; try { ms = new MongoServer(mongoSrv); CommandResult mcr = getMongoData(ms.getHost(), ms.getPort()); if (isValidData(mcr)) { MetricFeedBundle mfb = makeMetrics(mcr); deliverMetrics(mfb); } } catch (Exception e) { logger.log(Level.SEVERE, "Exception: ", e); } } }
private CommandResult runDBCmd( final String host, final int port, final String database, final String cmd ) throws Exception { MongoClient dbClient = null; try { dbClient = setupDbClient(host, port); DB db = dbClient.getDB(database); return db.command(cmd); } finally { if (dbClient != null) { dbClient.close(); } } }
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")); } // 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; }
@Override public List<DependencyStatus> status() { //note failures are tested manually for now, if you make changes test //things still work //TODO TEST add tests exercising failures final String version; try { final CommandResult bi = gfs.getDB().command("buildInfo"); version = bi.getString("version"); } catch (MongoException e) { LoggerFactory.getLogger(getClass()) .error("Failed to connect to MongoDB", e); return Arrays.asList(new DependencyStatus(false, "Couldn't connect to MongoDB: " + e.getMessage(), "GridFS", "Unknown")); } return Arrays.asList( new DependencyStatus(true, "OK", "GridFS", version)); }
public List<DependencyStatus> status() { //note failures are tested manually for now, if you make changes test //things still work //TODO TEST add tests exercising failures final List<DependencyStatus> deps = new LinkedList<>(blob.status()); final String version; try { final CommandResult bi = wsmongo.command("buildInfo"); version = bi.getString("version"); } catch (MongoException e) { LoggerFactory.getLogger(getClass()) .error("Failed to connect to MongoDB", e); deps.add(0, new DependencyStatus(false, "Couldn't connect to MongoDB: " + e.getMessage(), "MongoDB", "Unknown")); return deps; } deps.add(0, new DependencyStatus(true, "OK", "MongoDB", version)); return deps; }
private void setup() throws UnknownHostException { if (isInitialized()) { mongoClient = new MongoClient(mongoUri); } try { CommandResult res = mongoClient.getDB("admin").command("buildInfo"); Object _version = res.get("version"); if (_version != null && _version instanceof String) { serverVersion = (String) _version; } else { LOGGER.warn("Cannot get the MongoDb version."); serverVersion = "3.x?"; } } catch (Throwable t) { LOGGER.warn("Cannot get the MongoDb version."); serverVersion = "?"; } }
/** * set the state of balancer. * * @param dbConn * @param state * @return Error description, or null if no errors */ private static String setBalancerState(DB dbConn, boolean state) { DBObject balancer = new BasicDBObject(ID, "balancer"); DBObject updateObj = new BasicDBObject(); String stopped = state ? "false" : "true"; updateObj.put("$set", new BasicDBObject("stopped", stopped)); WriteResult wresult = dbConn.getSisterDB("config").getCollection("settings").update(balancer, updateObj, true, false); if (wresult != null) { CommandResult result = wresult.getLastError(); if (!result.ok()) { LOG.error("Error setting balancer state to {}: {}", state, result.getErrorMessage()); return result.getErrorMessage(); } } return null; }
private int countSubDocs(DBObject parentQuery) { simplifyParentQuery(parentQuery); DBObject idQuery = buildIdQuery(parentQuery); // String queryCommand = buildAggregateQuery((idQuery == null ? parentQuery.toString() : idQuery.toString()), // parentQuery.toString(), ", {$group: { _id: null, count: {$sum: 1}}}"); String groupQuery = ", {$group: { _id: null, count: {$sum: 1}}}"; String queryCommand; if (idQuery == null) { queryCommand = buildAggregateQuery(parentQuery.toString(), null, groupQuery); } else { queryCommand = buildAggregateQuery(idQuery.toString(), parentQuery.toString(), groupQuery); } TenantContext.setIsSystemCall(false); CommandResult result = template.executeCommand(queryCommand); @SuppressWarnings("unchecked") Iterator<DBObject> resultList = ((List<DBObject>) result.get("result")).iterator(); if (resultList.hasNext()) { return (Integer) (resultList.next().get("count")); } else { return 0; } }
@Before public void setup() { studentSectionAssociation.put("sectionId", SECTION1); studentSectionAssociation.put("studentId", STUDENT1); studentSectionAssociation.put("beginDate", BEGINDATE); studentSectionAssociation.put("endDate", ENDDATE1); WriteResult success = mock(WriteResult.class); CommandResult successCR = mock(CommandResult.class); CommandResult failCR = mock(CommandResult.class); when(success.getLastError()).thenReturn(successCR); when(successCR.ok()).thenReturn(true); when(successCR.get("value")).thenReturn("updated"); when(failCR.get("value")).thenReturn(null); when(failCR.get("result")).thenReturn(null); when(studentCollection.update(any(DBObject.class), any(DBObject.class), eq(false), eq(true), eq(WriteConcern.SAFE))).thenReturn( success); when(studentCollection.update(any(DBObject.class), any(DBObject.class), eq(true), eq(true), eq(WriteConcern.SAFE))).thenReturn( success); when(template.getCollection("student")).thenReturn(studentCollection); Query query = new Query(); query.addCriteria(Criteria.where("_id").is(SSAID)); MongoEntity entity = new MongoEntity("studentSectionAssociation", studentSectionAssociation); when(template.findOne(eq(query), eq(Entity.class), eq("studentSectionAssociation"))).thenReturn(entity); }
@Override public void execute() throws Exception { MongoClient mdb = MongoFactory.getInst().getMongo( sName ); if ( mdb == null ) throw new Exception("no server selected"); setDBNames(mdb); Iterator<String> it = dbNames.iterator(); while ( it.hasNext() ){ String db = it.next(); CommandResult cmdr = mdb.getDB(db).getStats(); statsListMap.add( transform(cmdr.toMap()) ); } setMessage("Retrived Database Stats; db=" + statsListMap.size() ); }
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(); } } } }
@Override protected void doHealthCheck(Health.Builder builder) throws Exception { final CommandResult buildInfo = mongoTemplate.executeCommand("{ buildInfo: 1 }"); final DbVersion dbVersion = mongoTemplate.findById(SMARTI_DB_VERSION_ID, DbVersion.class, COLLECTION_NAME); builder.up() .withDetail("version", buildInfo.getString("version")) .withDetail("db-version", dbVersion.version) ; }
@Test @DirtiesContext // because we can't authenticate twice on same DB public void deleteUserSucceeds() throws MongoServiceException { service.createDatabase(DB_NAME); DBObject createUserCmd = BasicDBObjectBuilder.start("createUser", "user").add("pwd", "password") .add("roles", new BasicDBList()).get(); CommandResult result = client.getDB(DB_NAME).command(createUserCmd); assertTrue("create should succeed", result.ok()); service.deleteUser(DB_NAME, "user"); assertFalse(client.getDB(DB_NAME).authenticate("user", "password".toCharArray())); }
@Override protected Result check() throws Exception { MongoClient mongoClient = null; String databaseList = null; String databaseStats = null; try { mongoClient = createMongoClient(); MongoIterable<String> dbList = mongoClient.listDatabaseNames(); databaseList = StringUtils.join(dbList, ','); CommandResult resultSet = mongoClient.getDB(databaseName).getStats(); databaseStats = resultSet.toString(); logger.debug("connectionUrl={} databaseList={} stats={}", connectionUrl, databaseList, databaseStats); Integer nbrCollections = (Integer) resultSet.get("collections"); if (nbrCollections == 0) { throw new RuntimeException("Database has nothing in it."); } } catch (Exception e) { ContextedRuntimeException wrappedException = wrapException(e); wrappedException.addContextValue("databaseList", databaseList); wrappedException.addContextValue("databaseStats", databaseStats); logger.error("MongoDB Healthcheck Failure", wrappedException); return Result.unhealthy(wrappedException); } finally { closeQuietly(mongoClient); } return Result.healthy(); }
@Test public void mongoIsUp() throws Exception { CommandResult commandResult = mock(CommandResult.class); given(commandResult.getString("version")).willReturn("2.6.4"); MongoTemplate mongoTemplate = mock(MongoTemplate.class); given(mongoTemplate.executeCommand("{ buildInfo: 1 }")).willReturn(commandResult); MongoHealthIndicator healthIndicator = new MongoHealthIndicator(mongoTemplate); Health health = healthIndicator.health(); assertThat(health.getStatus()).isEqualTo(Status.UP); assertThat(health.getDetails().get("version")).isEqualTo("2.6.4"); verify(commandResult).getString("version"); verify(mongoTemplate).executeCommand("{ buildInfo: 1 }"); }
@Override protected Result check() throws Exception { try { final CommandResult stats = datastore.getDB().getStats(); if (!stats.ok()) return Result.unhealthy(stats.getErrorMessage()); } catch (MongoException ex) { return Result.unhealthy(ex.getMessage()); } return Result.healthy(); }
@Override public Health health() { Health.Builder healthBuilder = new Health.Builder(); try { CommandResult result = this.mongoTemplate.executeCommand("{ ping: 1 }"); healthBuilder.up().withDetail("server", result.get("serverUsed")); } catch (Exception ex) { healthBuilder.down(ex); } return healthBuilder.build(); }
@Test public void mongoIsUp() throws Exception { CommandResult commandResult = mock(CommandResult.class); given(commandResult.getString("version")).willReturn("2.6.4"); MongoTemplate mongoTemplate = mock(MongoTemplate.class); given(mongoTemplate.executeCommand("{ buildInfo: 1 }")).willReturn(commandResult); MongoHealthIndicator healthIndicator = new MongoHealthIndicator(mongoTemplate); Health health = healthIndicator.health(); assertEquals(Status.UP, health.getStatus()); assertEquals("2.6.4", health.getDetails().get("version")); verify(commandResult).getString("version"); verify(mongoTemplate).executeCommand("{ buildInfo: 1 }"); }
@Override protected Result check() throws Exception { final BasicDBObject command = new BasicDBObject("buildInfo", 1); final CommandResult commandResult = db.command(command); if (!commandResult.ok()) { return Result.unhealthy(commandResult.getErrorMessage()); } final String version = commandResult.getString("version"); return Result.healthy("Mongo server version: %s", version); }
@Override protected void doHealthCheck(Health.Builder builder) throws Exception { try { CommandResult result = mongoTemplate.executeCommand("{ serverStatus: 1 }"); if (result.ok()) { builder.up().withDetail("mongodb", "ok") .withDetail("version", result.getString("version")); } else { builder.down().withDetail("mongodb", "error"); } } catch (Exception e) { builder.down(e); } }
public static void testMyUserId(int max, DB db) { String collName = "testmyuserid"; DBCollection coll = db.getCollection(collName); //Setup a sharded collection BasicDBObject command = new BasicDBObject(); command.put("shardcollection", collName); DBObject key = new BasicDBObject(); key.put("_id", 1); command.put("key", key); command.put("unique", true); db.command(command); long startM = System.currentTimeMillis(); BasicDBObject obj = new BasicDBObject(); for ( int i=0; i<max; i++ ) { UserId userId = new UserId("username"+i); obj.put("_id", userId.getInternal()); obj.put("test", "value-"+i); coll.save(obj); } long endM = System.currentTimeMillis(); System.out.println("Insert " + max + " my objectid. time: " + (endM-startM) + " benchmark()"); CommandResult result = db.getStats(); System.out.println(result); }
public static void testStringUserId(int max, DB db) { String collName = "teststringid"; DBCollection coll = db.getCollection(collName); //Setup a sharded collection BasicDBObject command = new BasicDBObject(); command.put("shardcollection", collName); DBObject key = new BasicDBObject(); key.put("_id", 1); command.put("key", key); command.put("unique", true); db.command(command); long startM = System.currentTimeMillis(); BasicDBObject obj = new BasicDBObject(); for ( int i=0; i<max; i++ ) { obj.put("_id", "username"+i); obj.put("test", "value-"+i); coll.save(obj); } long endM = System.currentTimeMillis(); System.out.println("Insert " + max + " my objectid. time: " + (endM-startM) + " benchmark()"); CommandResult result = db.getStats(); System.out.println(result); }
public static void testBasicBson(int max, DB db) { String collName = "testbasicbson"; DBCollection coll = db.getCollection(collName); //Setup a sharded collection BasicDBObject command = new BasicDBObject(); command.put("shardcollection", collName); DBObject key = new BasicDBObject(); key.put("_id", 1); command.put("key", key); command.put("unique", true); db.command(command); long startM = System.currentTimeMillis(); BasicDBObject objKey = new BasicDBObject(); UserId userId = new UserId("username"); objKey.put("_id", userId.getInternal()); BasicDBObject obj = new BasicDBObject(); for ( int i=0; i<max; i++ ) { obj.put("_id", userId.getInternal()); obj.put("test-"+(i)%10, "value-"+i); coll.update(objKey, obj, true, false); } long endM = System.currentTimeMillis(); System.out.println(collName+ " update " + max + " my objectid. time: " + (endM-startM) + " benchmark(56273)"); CommandResult result = db.getStats(); System.out.println(result); }
public static void testMapDBObject(int max, DB db) { String collName = "testmapobject"; DBCollection coll = db.getCollection(collName); //Setup a sharded collection BasicDBObject command = new BasicDBObject(); command.put("shardcollection", collName); DBObject key = new BasicDBObject(); key.put("_id", 1); command.put("key", key); command.put("unique", true); db.command(command); long startM = System.currentTimeMillis(); BasicDBObject objKey = new BasicDBObject(); UserId userId = new UserId("username"); objKey.put("_id", userId.getInternal()); MapDBObject obj = new MapDBObject(); for ( int i=0; i<max; i++ ) { obj.put("_id", userId.getInternal()); obj.put("test-"+(i)%10, "value-"+i); coll.update(objKey, obj, true, false); } long endM = System.currentTimeMillis(); System.out.println(collName+ " update " + max + " my objectid. time: " + (endM-startM) + " benchmark(114892)"); CommandResult result = db.getStats(); System.out.println(result); }
@Test public void testDoEval() { int x = 3; int y = 5; String code = "function(x,y) {return x+y}"; CommandResult result = MongoDBUtil.doEval(testDB, null, "mongoutil", code, new Object[]{x, y}); //{ "serverUsed" : "mongos.babywar.xinqihd.com:27017" , "retval" : 8.0 , "ok" : 1.0} System.out.println(result); assertEquals(8, result.getInt("retval")); }
@Test public void testCopyCollection() { String sourceNamespace = "test"; String sourceCollection = "mongoutil"; String targetNamespace = "test"; String targetCollection = "mongoutiltest"; DBObject query = MongoDBUtil.createDBObject(); int count = 10; for ( int i=0; i<count; i++ ) { DBObject dbObject = new BasicDBObject(); dbObject.put("_id", i); dbObject.put("name", "value"+i); MongoDBUtil.saveToMongo(dbObject, dbObject, testDB, sourceNamespace, sourceCollection, true); } List<DBObject> list = MongoDBUtil.queryAllFromMongo(query, testDB, sourceNamespace, sourceCollection, null); assertEquals(count, list.size()); MongoDBUtil.removeDocument(testDB, targetNamespace, targetCollection, null); String code = "function(sourceDatabase, sourceNamespace, sourceCollection, targetDatabase, targetNamespace, targetCollection) { \n"+ " var currentdb = db.getSisterDB(sourceDatabase); \n"+ " var gamedb = db.getSisterDB(targetDatabase); \n"+ " currentdb.getCollection(sourceNamespace+\".\"+sourceCollection).find().forEach(function(x){gamedb.getCollection(targetNamespace+\".\"+targetCollection).insert(x)}); \n"+ " return sourceNamespace;\n"+ "}"; CommandResult result = MongoDBUtil.doEval( testDB, sourceNamespace, sourceCollection, code, new Object[]{testDB, sourceNamespace, sourceCollection, testDB, targetNamespace, targetCollection}); System.out.println(result); list = MongoDBUtil.queryAllFromMongo(query, testDB, targetNamespace, targetCollection, null); assertEquals(count, list.size()); }
private CommandResult dbAdminCmd( final String host, final int port, final String cmd ) throws Exception { return runDBCmd(host, port, "admin", cmd); }