Search in sources :

Example 1 with ReplSetHeartbeatArgument

use of com.torodb.mongodb.commands.signatures.internal.ReplSetHeartbeatCommand.ReplSetHeartbeatArgument in project torodb by torodb.

the class TopologyCoordinator method prepareHeartbeatRequest.

/**
   * Prepares a heartbeat request appropriate for sending to "target", assuming the current time is
   * "now".
   * <p>
   * The returned pair contains proper arguments for a replSetHeartbeat command, and an amount of
   * time to wait for the response.
   * <p>
   * This call should be paired (with intervening network communication) with a call to
   * processHeartbeatResponse for the same "target".
   *
   * @param now        our current time
   * @param ourSetName is used as the name for our replica set if the topology coordinator does not
   *                   have a valid configuration installed.
   * @param host       the target of the request to be created
   */
RemoteCommandRequest<ReplSetHeartbeatArgument> prepareHeartbeatRequest(Instant now, String ourSetName, HostAndPort target) {
    PingStats hbStats = getPingOrDefault(target);
    Duration alreadyElapsed;
    if (hbStats.getLastHeartbeatStartDate() != null) {
        alreadyElapsed = Duration.between(hbStats.getLastHeartbeatStartDate(), now);
    } else {
        alreadyElapsed = Duration.between(Instant.EPOCH, now);
    }
    if (_rsConfig == null || (hbStats.getNumFailuresSinceLastStart() > MAX_HEARTBEAT_RETRIES) || (alreadyElapsed.toMillis() >= _rsConfig.getHeartbeatTimeoutPeriod())) {
        // This is either the first request ever for "target", or the heartbeat timeout has
        // passed, so we're starting a "new" heartbeat.
        hbStats.start(now);
        alreadyElapsed = Duration.ZERO;
    }
    ReplSetHeartbeatArgument.Builder hbArgs = new ReplSetHeartbeatArgument.Builder(ReplSetProtocolVersion.V1).setCheckEmpty(false);
    if (_rsConfig != null) {
        hbArgs.setSetName(_rsConfig.getReplSetName());
        hbArgs.setConfigVersion(_rsConfig.getConfigVersion());
    } else {
        hbArgs.setSetName(ourSetName);
        hbArgs.setConfigVersion(-2);
    }
    final Duration timeoutPeriod = _rsConfig != null ? Duration.ofMillis(_rsConfig.getHeartbeatTimeoutPeriod()) : HEARTBEAT_INTERVAL;
    Duration timeout = timeoutPeriod.minus(alreadyElapsed);
    return new RemoteCommandRequest<>(target, "admin", hbArgs.build(), timeout);
}
Also used : Duration(java.time.Duration) ReplSetHeartbeatArgument(com.torodb.mongodb.commands.signatures.internal.ReplSetHeartbeatCommand.ReplSetHeartbeatArgument)

Example 2 with ReplSetHeartbeatArgument

use of com.torodb.mongodb.commands.signatures.internal.ReplSetHeartbeatCommand.ReplSetHeartbeatArgument in project torodb by torodb.

the class TopologyHeartbeatHandler method doHeartbeat.

@GuardedBy("executor")
private void doHeartbeat(final TopologyCoordinator coord, final HostAndPort target) {
    if (stopped) {
        LOGGER.trace("Ignoring heartbeat to {} because the handler has " + "been stopped", target);
        return;
    }
    Instant start = clock.instant();
    RemoteCommandRequest<ReplSetHeartbeatArgument> request = coord.prepareHeartbeatRequest(start, replSetName, target);
    CompletableFuture<RemoteCommandResponse<ReplSetHeartbeatReply>> hbHandle = networkHandler.sendHeartbeat(request).exceptionally(t -> onNetworkError(t, target, start));
    executor.onCurrentVersion().andThenAcceptAsync(hbHandle, (coord2, response) -> handleHeartbeatResponse(coord2, target, request.getCmdObj(), response));
}
Also used : Instant(java.time.Instant) RemoteCommandResponse(com.eightkdata.mongowp.client.core.MongoConnection.RemoteCommandResponse) ErroneousRemoteCommandResponse(com.eightkdata.mongowp.client.core.MongoConnection.ErroneousRemoteCommandResponse) ReplSetHeartbeatArgument(com.torodb.mongodb.commands.signatures.internal.ReplSetHeartbeatCommand.ReplSetHeartbeatArgument) GuardedBy(javax.annotation.concurrent.GuardedBy)

Aggregations

ReplSetHeartbeatArgument (com.torodb.mongodb.commands.signatures.internal.ReplSetHeartbeatCommand.ReplSetHeartbeatArgument)2 ErroneousRemoteCommandResponse (com.eightkdata.mongowp.client.core.MongoConnection.ErroneousRemoteCommandResponse)1 RemoteCommandResponse (com.eightkdata.mongowp.client.core.MongoConnection.RemoteCommandResponse)1 Duration (java.time.Duration)1 Instant (java.time.Instant)1 GuardedBy (javax.annotation.concurrent.GuardedBy)1