Search in sources :

Example 36 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 37 with ServerAddress

use of com.mongodb.ServerAddress in project mongo-java-driver by mongodb.

the class AbstractServerDiscoveryAndMonitoringTest method applyResponse.

protected void applyResponse(final BsonArray response) {
    ServerAddress serverAddress = new ServerAddress(response.get(0).asString().getValue());
    BsonDocument isMasterResult = response.get(1).asDocument();
    ServerDescription serverDescription;
    if (isMasterResult.isEmpty()) {
        serverDescription = ServerDescription.builder().type(ServerType.UNKNOWN).state(CONNECTING).address(serverAddress).build();
    } else {
        serverDescription = createServerDescription(serverAddress, isMasterResult, getVersion(new BsonDocument("versionArray", new BsonArray(asList(new BsonInt32(2), new BsonInt32(6), new BsonInt32(0))))), 5000000);
    }
    factory.sendNotification(serverAddress, serverDescription);
}
Also used : BsonInt32(org.bson.BsonInt32) BsonDocument(org.bson.BsonDocument) DescriptionHelper.createServerDescription(com.mongodb.connection.DescriptionHelper.createServerDescription) BsonArray(org.bson.BsonArray) ServerAddress(com.mongodb.ServerAddress)

Example 38 with ServerAddress

use of com.mongodb.ServerAddress in project mongo-java-driver by mongodb.

the class ClusterDescriptionTest method testObjectOverrides.

@Test
public void testObjectOverrides() throws UnknownHostException {
    ClusterDescription description = new ClusterDescription(MULTIPLE, UNKNOWN, asList(builder().state(CONNECTING).address(new ServerAddress("loc:27019")).lastUpdateTimeNanos(42L).build(), builder().state(CONNECTING).address(new ServerAddress("loc:27018")).lastUpdateTimeNanos(42L).build(), builder().state(CONNECTING).address(new ServerAddress("loc:27017")).lastUpdateTimeNanos(42L).build()));
    ClusterDescription descriptionTwo = new ClusterDescription(MULTIPLE, UNKNOWN, asList(builder().state(CONNECTING).address(new ServerAddress("loc:27019")).lastUpdateTimeNanos(42L).build(), builder().state(CONNECTING).address(new ServerAddress("loc:27018")).lastUpdateTimeNanos(42L).build(), builder().state(CONNECTING).address(new ServerAddress("loc:27017")).lastUpdateTimeNanos(42L).build()));
    assertEquals(description, descriptionTwo);
    assertEquals(description.hashCode(), descriptionTwo.hashCode());
    assertTrue(description.toString().startsWith("ClusterDescription"));
}
Also used : ServerAddress(com.mongodb.ServerAddress) Test(org.junit.Test)

Example 39 with ServerAddress

use of com.mongodb.ServerAddress in project mongo-java-driver by mongodb.

the class PlainAuthenticatorUnitTest method before.

@Before
public void before() {
    connection = new TestInternalConnection(new ServerId(new ClusterId(), new ServerAddress("localhost", 27017)));
    connectionDescription = new ConnectionDescription(new ServerId(new ClusterId(), new ServerAddress()));
    credential = MongoCredential.createPlainCredential("user", "$external", "pencil".toCharArray());
    subject = new PlainAuthenticator(this.credential);
}
Also used : ServerAddress(com.mongodb.ServerAddress) Before(org.junit.Before)

Example 40 with ServerAddress

use of com.mongodb.ServerAddress in project mongo-java-driver by mongodb.

the class ScramSha1SaslAuthenticatorTest method before.

@Before
public void before() {
    this.connection = new TestInternalConnection(new ServerId(new ClusterId(), new ServerAddress("localhost", 27017)));
    connectionDescription = new ConnectionDescription(new ServerId(new ClusterId(), new ServerAddress()));
    this.credential = MongoCredential.createScramSha1Credential("user", "database", "pencil".toCharArray());
    ScramSha1Authenticator.RandomStringGenerator randomStringGenerator = new ScramSha1Authenticator.RandomStringGenerator() {

        @Override
        public String generate(final int length) {
            return "fyko+d2lbbFgONRv9qkxdawL";
        }
    };
    this.subject = new ScramSha1Authenticator(this.credential, randomStringGenerator);
}
Also used : ServerAddress(com.mongodb.ServerAddress) Before(org.junit.Before)

Aggregations

ServerAddress (com.mongodb.ServerAddress)72 Test (org.junit.Test)27 MongoClient (com.mongodb.MongoClient)21 ArrayList (java.util.ArrayList)13 MongoCredential (com.mongodb.MongoCredential)10 Before (org.junit.Before)9 Document (org.bson.Document)5 TagSet (com.mongodb.TagSet)4 ClusterDescription (com.mongodb.connection.ClusterDescription)4 ClusterSettings (com.mongodb.connection.ClusterSettings)4 MongoClient (com.mongodb.reactivestreams.client.MongoClient)4 UnknownHostException (java.net.UnknownHostException)4 List (java.util.List)4 Tag (com.mongodb.Tag)3 MongoDatabase (com.mongodb.client.MongoDatabase)3 ServerDescription (com.mongodb.connection.ServerDescription)3 Date (java.util.Date)3 HashSet (java.util.HashSet)3 ChunkInfo (org.apache.drill.exec.store.mongo.common.ChunkInfo)3 BsonDocument (org.bson.BsonDocument)3