Search in sources :

Example 51 with ServerAddress

use of com.mongodb.ServerAddress in project spring-cloud-connectors by spring-cloud.

the class MongoServiceConnectorCreatorTest method cloudMongoCreationNoConfig.

@Test
public void cloudMongoCreationNoConfig() throws Exception {
    MongoServiceInfo serviceInfo = new MongoServiceInfo("id", TEST_HOST, TEST_PORT, TEST_USERNAME, TEST_PASSWORD, TEST_DB);
    MongoDbFactory mongoDbFactory = testCreator.create(serviceInfo, null);
    assertNotNull(mongoDbFactory);
    MongoClient mongo = (MongoClient) ReflectionTestUtils.getField(mongoDbFactory, "mongo");
    assertNotNull(mongo);
    MongoCredential credentials = mongo.getCredentialsList().get(0);
    List<ServerAddress> addresses = extractServerAddresses(mongo);
    assertEquals(1, addresses.size());
    ServerAddress address = addresses.get(0);
    assertEquals(serviceInfo.getHost(), address.getHost());
    assertEquals(serviceInfo.getPort(), address.getPort());
    assertEquals(serviceInfo.getUserName(), credentials.getUserName());
    assertNotNull(credentials.getPassword());
    // Don't do connector.getDatabase().getName() as that will try to initiate the connection
    assertEquals(serviceInfo.getDatabase(), ReflectionTestUtils.getField(mongoDbFactory, "databaseName"));
}
Also used : MongoClient(com.mongodb.MongoClient) MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) MongoCredential(com.mongodb.MongoCredential) ServerAddress(com.mongodb.ServerAddress) MongoServiceInfo(org.springframework.cloud.service.common.MongoServiceInfo) Test(org.junit.Test)

Example 52 with ServerAddress

use of com.mongodb.ServerAddress in project graylog2-server by Graylog2.

the class MongoProbe method mongoStats.

public MongoStats mongoStats() {
    final List<ServerAddress> serverAddresses = mongoClient.getServerAddressList();
    final List<HostAndPort> servers = Lists.newArrayListWithCapacity(serverAddresses.size());
    for (ServerAddress serverAddress : serverAddresses) {
        servers.add(HostAndPort.fromParts(serverAddress.getHost(), serverAddress.getPort()));
    }
    final DatabaseStats dbStats;
    final CommandResult dbStatsResult = db.command("dbStats");
    if (dbStatsResult.ok()) {
        final BasicDBObject extentFreeListMap = (BasicDBObject) dbStatsResult.get("extentFreeList");
        final DatabaseStats.ExtentFreeList extentFreeList;
        if (extentFreeListMap == null) {
            extentFreeList = null;
        } else {
            extentFreeList = DatabaseStats.ExtentFreeList.create(extentFreeListMap.getInt("num"), extentFreeListMap.getInt("totalSize"));
        }
        final BasicDBObject dataFileVersionMap = (BasicDBObject) dbStatsResult.get("dataFileVersion");
        final DatabaseStats.DataFileVersion dataFileVersion;
        if (dataFileVersionMap == null) {
            dataFileVersion = null;
        } else {
            dataFileVersion = DatabaseStats.DataFileVersion.create(dataFileVersionMap.getInt("major"), dataFileVersionMap.getInt("minor"));
        }
        dbStats = DatabaseStats.create(dbStatsResult.getString("db"), dbStatsResult.getLong("collections"), dbStatsResult.getLong("objects"), dbStatsResult.getDouble("avgObjSize"), dbStatsResult.getLong("dataSize"), dbStatsResult.getLong("storageSize"), dbStatsResult.getLong("numExtents"), dbStatsResult.getLong("indexes"), dbStatsResult.getLong("indexSize"), dbStatsResult.containsField("fileSize") ? dbStatsResult.getLong("fileSize") : null, dbStatsResult.containsField("nsSizeMB") ? dbStatsResult.getLong("nsSizeMB") : null, extentFreeList, dataFileVersion);
    } else {
        LOG.debug("Couldn't retrieve MongoDB dbStats: {}", dbStatsResult.getErrorMessage());
        dbStats = null;
    }
    final ServerStatus serverStatus;
    final CommandResult serverStatusResult = adminDb.command("serverStatus");
    if (serverStatusResult.ok()) {
        final BasicDBObject connectionsMap = (BasicDBObject) serverStatusResult.get("connections");
        final ServerStatus.Connections connections = ServerStatus.Connections.create(connectionsMap.getInt("current"), connectionsMap.getInt("available"), connectionsMap.containsField("totalCreated") ? connectionsMap.getLong("totalCreated") : null);
        final BasicDBObject networkMap = (BasicDBObject) serverStatusResult.get("network");
        final ServerStatus.Network network = ServerStatus.Network.create(networkMap.getInt("bytesIn"), networkMap.getInt("bytesOut"), networkMap.getInt("numRequests"));
        final BasicDBObject memoryMap = (BasicDBObject) serverStatusResult.get("mem");
        final ServerStatus.Memory memory = ServerStatus.Memory.create(memoryMap.getInt("bits"), memoryMap.getInt("resident"), memoryMap.getInt("virtual"), memoryMap.getBoolean("supported"), memoryMap.getInt("mapped"), memoryMap.getInt("mappedWithJournal", -1));
        final BasicDBObject storageEngineMap = (BasicDBObject) serverStatusResult.get("storageEngine");
        final ServerStatus.StorageEngine storageEngine;
        if (storageEngineMap == null) {
            storageEngine = ServerStatus.StorageEngine.DEFAULT;
        } else {
            storageEngine = ServerStatus.StorageEngine.create(storageEngineMap.getString("name"));
        }
        final int uptime = serverStatusResult.getInt("uptime", 0);
        serverStatus = ServerStatus.create(serverStatusResult.getString("host"), serverStatusResult.getString("version"), serverStatusResult.getString("process"), serverStatusResult.getLong("pid", 0), uptime, serverStatusResult.getLong("uptimeMillis", uptime * 1000L), serverStatusResult.getInt("uptimeEstimate"), new DateTime(serverStatusResult.getDate("localTime")), connections, network, memory, storageEngine);
    } else {
        LOG.debug("Couldn't retrieve MongoDB serverStatus: {}", serverStatusResult.getErrorMessage());
        serverStatus = null;
    }
    // TODO Collection stats? http://docs.mongodb.org/manual/reference/command/collStats/
    return MongoStats.create(servers, buildInfo, hostInfo, serverStatus, dbStats);
}
Also used : ServerAddress(com.mongodb.ServerAddress) DateTime(org.joda.time.DateTime) CommandResult(com.mongodb.CommandResult) HostAndPort(com.google.common.net.HostAndPort) BasicDBObject(com.mongodb.BasicDBObject)

Example 53 with ServerAddress

use of com.mongodb.ServerAddress in project MonjaDB by Kanatoko.

the class MMongoUtil method getReplMongo.

//--------------------------------------------------------------------------------
public static Mongo getReplMongo(String mongoStr, MongoOptions options) throws UnknownHostException {
    if (mongoStr == null || mongoStr.equals("")) {
        mongoStr = "127.0.0.1";
    }
    List addrList = new ArrayList();
    String[] array = mongoStr.split(",");
    for (int i = 0; i < array.length; ++i) {
        int port = 27017;
        String host = "";
        int index = array[i].indexOf(":");
        if (index == -1) {
            host = array[i];
        } else {
            host = array[i].substring(0, index);
            port = MStringUtil.parseInt(array[i].substring(index), 27017);
        }
        addrList.add(new ServerAddress(host, port));
    }
    if (options == null) {
        return new Mongo(addrList);
    } else {
        return new Mongo(addrList, options);
    }
}
Also used : ServerAddress(com.mongodb.ServerAddress)

Example 54 with ServerAddress

use of com.mongodb.ServerAddress in project drill by apache.

the class MongoGroupScan method handleUnshardedCollection.

private void handleUnshardedCollection(List<String> hosts) {
    String chunkName = Joiner.on('.').join(scanSpec.getDbName(), scanSpec.getCollectionName());
    Set<ServerAddress> addressList = Sets.newHashSet();
    for (String host : hosts) {
        addressList.add(new ServerAddress(host));
    }
    chunksMapping.put(chunkName, addressList);
    String host = hosts.get(0);
    ServerAddress address = new ServerAddress(host);
    ChunkInfo chunkInfo = new ChunkInfo(hosts, chunkName);
    chunkInfo.setMinFilters(Collections.<String, Object>emptyMap());
    chunkInfo.setMaxFilters(Collections.<String, Object>emptyMap());
    List<ChunkInfo> chunksList = Lists.newArrayList();
    chunksList.add(chunkInfo);
    chunksInverseMapping.put(address.getHost(), chunksList);
}
Also used : ChunkInfo(org.apache.drill.exec.store.mongo.common.ChunkInfo) ServerAddress(com.mongodb.ServerAddress)

Example 55 with ServerAddress

use of com.mongodb.ServerAddress in project drill by apache.

the class MongoStoragePlugin method getClient.

public MongoClient getClient() {
    List<String> hosts = clientURI.getHosts();
    List<ServerAddress> addresses = Lists.newArrayList();
    for (String host : hosts) {
        addresses.add(new ServerAddress(host));
    }
    return getClient(addresses);
}
Also used : ServerAddress(com.mongodb.ServerAddress)

Aggregations

ServerAddress (com.mongodb.ServerAddress)58 Test (org.junit.Test)22 MongoClient (com.mongodb.MongoClient)13 MongoCredential (com.mongodb.MongoCredential)9 ArrayList (java.util.ArrayList)9 Before (org.junit.Before)9 TagSet (com.mongodb.TagSet)4 ClusterDescription (com.mongodb.connection.ClusterDescription)4 ClusterSettings (com.mongodb.connection.ClusterSettings)4 MongoClient (com.mongodb.reactivestreams.client.MongoClient)4 Tag (com.mongodb.Tag)3 MongoDatabase (com.mongodb.client.MongoDatabase)3 ServerDescription (com.mongodb.connection.ServerDescription)3 UnknownHostException (java.net.UnknownHostException)3 Date (java.util.Date)3 ChunkInfo (org.apache.drill.exec.store.mongo.common.ChunkInfo)3 BsonDocument (org.bson.BsonDocument)3 DataXException (com.alibaba.datax.common.exception.DataXException)2 ConnectionString (com.mongodb.ConnectionString)2 MongoClientURI (com.mongodb.MongoClientURI)2