Search in sources :

Example 36 with HostAndPort

use of com.google.common.net.HostAndPort in project torodb by torodb.

the class TopologyService method chooseNewSyncSource.

public CompletableFuture<Optional<HostAndPort>> chooseNewSyncSource(Optional<OpTime> lastFetchedOpTime) {
    return executor.onAnyVersion().mapAsync(coord -> {
        Instant now = clock.instant();
        HostAndPort currentSyncSource = coord.getSyncSourceAddress().orElse(null);
        boolean shouldChange = currentSyncSource == null || coord.shouldChangeSyncSource(currentSyncSource, now);
        if (shouldChange) {
            return coord.chooseNewSyncSource(clock.instant(), lastFetchedOpTime);
        } else {
            return coord.getSyncSourceAddress();
        }
    });
}
Also used : HostAndPort(com.google.common.net.HostAndPort) Instant(java.time.Instant)

Example 37 with HostAndPort

use of com.google.common.net.HostAndPort in project torodb by torodb.

the class TopologyService method shouldChangeSyncSource.

CompletableFuture<Boolean> shouldChangeSyncSource() {
    return executor.onAnyVersion().mapAsync(coord -> {
        Instant now = clock.instant();
        HostAndPort currentSyncSource = coord.getSyncSourceAddress().orElse(null);
        return currentSyncSource == null || coord.shouldChangeSyncSource(currentSyncSource, now);
    });
}
Also used : HostAndPort(com.google.common.net.HostAndPort) Instant(java.time.Instant)

Example 38 with HostAndPort

use of com.google.common.net.HostAndPort in project torodb by torodb.

the class TopologyCoordinator method executeReplSetSyncFrom.

ReplSetSyncFromReply executeReplSetSyncFrom(ErrorCode status, HostAndPort target, OpTime lastOpApplied) throws MongoException {
    if (status == ErrorCode.CALLBACK_CANCELED) {
        throw new ShutdownInProgressException("replication system is shutting down");
    }
    final HostAndPort syncFromRequested = target;
    MemberConfig targetConfig = null;
    int targetIndex;
    for (targetIndex = 0; targetIndex < _rsConfig.getMembers().size(); targetIndex++) {
        MemberConfig it = _rsConfig.getMembers().get(targetIndex);
        if (it.getHostAndPort().equals(target)) {
            targetConfig = it;
            break;
        }
    }
    if (targetConfig == null) {
        throw new NodeNotFoundException("Could not find member \"" + target + "\" in replica set");
    }
    if (targetConfig.isArbiter()) {
        throw new InvalidOptionsException("Cannot sync from \"" + target + "\" because it is an arbiter");
    }
    String warning = null;
    MemberHeartbeatData hbdata = _hbdata.get(targetIndex);
    if (hbdata.isAuthIssue()) {
        throw new UnauthorizedException("not authorized to communicate with " + target);
    }
    if (hbdata.getHealth() == Health.UNREACHABLE) {
        throw new HostUnreachableException("I cannot reach the requested member: " + target);
    }
    assert hbdata.getOpTime() != null;
    if (hbdata.getOpTime().getSecs() + 10 < lastOpApplied.getSecs()) {
        LOGGER.warn("attempting to sync from {}, but its latest opTime is {} and ours is {} " + "so this may not work", target, hbdata.getOpTime().getSecs(), lastOpApplied.getSecs());
        warning = "requested member \"" + target + "\" is more than 10 seconds behind us";
    }
    HostAndPort prevSyncSource = getSyncSourceAddress().orElse(null);
    setForceSyncSourceIndex(targetIndex);
    return new ReplSetSyncFromReply(prevSyncSource, syncFromRequested, warning);
}
Also used : MemberHeartbeatData(com.torodb.mongodb.commands.pojos.MemberHeartbeatData) HostAndPort(com.google.common.net.HostAndPort) NodeNotFoundException(com.eightkdata.mongowp.exceptions.NodeNotFoundException) InvalidOptionsException(com.eightkdata.mongowp.exceptions.InvalidOptionsException) ShutdownInProgressException(com.eightkdata.mongowp.exceptions.ShutdownInProgressException) HostUnreachableException(com.eightkdata.mongowp.exceptions.HostUnreachableException) UnauthorizedException(com.eightkdata.mongowp.exceptions.UnauthorizedException) ReplSetSyncFromReply(com.torodb.mongodb.commands.signatures.repl.ReplSetSyncFromCommand.ReplSetSyncFromReply) MemberConfig(com.torodb.mongodb.commands.pojos.MemberConfig)

Example 39 with HostAndPort

use of com.google.common.net.HostAndPort in project torodb by torodb.

the class MemberConfig method fromDocument.

public static MemberConfig fromDocument(BsonDocument bson) throws TypesMismatchException, NoSuchKeyException, BadValueException {
    int id = BsonReaderTool.getNumeric(bson, "_id").intValue();
    HostAndPort host = BsonReaderTool.getHostAndPort(bson, "host");
    Builder builder = new Builder(id, host).setVotes(BsonReaderTool.getInteger(bson, "votes", DEFAULT_VOTES)).setPriority(BsonReaderTool.getDouble(bson, "priority", DEFAULT_PRIORITY)).setArbiterOnly(BsonReaderTool.getBooleanOrNumeric(bson, "arbiterOnly", DEFAULT_ARBITER_ONLY)).setSlaveDelay(BsonReaderTool.getNumeric(bson, "slaveDelay", DEFAULT_SLAVE_DELAY).longValue()).setHidden(BsonReaderTool.getBooleanOrNumeric(bson, "hidden", DEFAULT_HIDDEN)).setBuildIndexes(BsonReaderTool.getBooleanOrNumeric(bson, "buildIndexes", DEFAULT_BUILD_INDEXES));
    BsonDocument castedTags = BsonReaderTool.getDocument(bson, "tags");
    for (Entry entry : castedTags) {
        BsonValue value = entry.getValue();
        if (value.isString()) {
            throw new TypesMismatchException(entry.getKey(), "string", value.getType());
        }
        String castedValue = value.asString().getValue();
        builder.putTag(entry.getKey(), castedValue);
    }
    return builder.build();
}
Also used : HostAndPort(com.google.common.net.HostAndPort) Entry(com.eightkdata.mongowp.bson.BsonDocument.Entry) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) BsonDocumentBuilder(com.eightkdata.mongowp.utils.BsonDocumentBuilder) TypesMismatchException(com.eightkdata.mongowp.exceptions.TypesMismatchException) BsonValue(com.eightkdata.mongowp.bson.BsonValue)

Example 40 with HostAndPort

use of com.google.common.net.HostAndPort 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)

Aggregations

HostAndPort (com.google.common.net.HostAndPort)46 IOException (java.io.IOException)8 Test (org.junit.Test)7 InetSocketAddress (java.net.InetSocketAddress)5 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 Request (com.metamx.http.client.Request)3 SequenceInputStreamResponseHandler (com.metamx.http.client.response.SequenceInputStreamResponseHandler)3 SystemException (com.torodb.core.exceptions.SystemException)3 JCommander (com.beust.jcommander.JCommander)2 Console (com.beust.jcommander.internal.Console)2 OpTime (com.eightkdata.mongowp.OpTime)2 UnreachableMongoServerException (com.eightkdata.mongowp.client.core.UnreachableMongoServerException)2 MongoException (com.eightkdata.mongowp.exceptions.MongoException)2 Charsets (com.google.common.base.Charsets)2 Throwables (com.google.common.base.Throwables)2 Service (com.google.common.util.concurrent.Service)2 CreationException (com.google.inject.CreationException)2 NoSyncSourceFoundException (com.torodb.mongodb.repl.exceptions.NoSyncSourceFoundException)2 BackendPasswordConfig (com.torodb.packaging.config.model.backend.BackendPasswordConfig)2