Search in sources :

Example 1 with BackoffTimer

use of com.linkedin.databus2.core.BackoffTimer in project databus by linkedin.

the class RelayPullThread method writeEventToRelayDispatcher.

private void writeEventToRelayDispatcher(ConnectionState curState, DbusEvent event, String message) throws InterruptedException, InvalidEventException {
    boolean success = false;
    // Create a infinite backoff timer that waits for maximum of 1 sec
    // for writing the control message to evb
    BackoffTimerStaticConfig timerConfig = new BackoffTimerStaticConfig(1, 1000, 1, 1, -1);
    BackoffTimer timer = new BackoffTimer("EVB More Space Timer", timerConfig);
    timer.reset();
    byte[] eventBytes = new byte[event.size()];
    _log.info("Event size: " + eventBytes.length);
    _log.info("Event:" + event.toString());
    event.getRawBytes().get(eventBytes);
    UnifiedClientStats unifiedClientStats = _sourcesConn.getUnifiedClientStats();
    while ((!success) && (timer.getRemainingRetriesNum() > 0)) {
        ByteArrayInputStream cpIs = new ByteArrayInputStream(eventBytes);
        ReadableByteChannel cpRbc = Channels.newChannel(cpIs);
        sendHeartbeat(unifiedClientStats);
        int ecnt = curState.getDataEventsBuffer().readEvents(cpRbc);
        if (ecnt <= 0) {
            _log.error("Not enough spece in the event buffer to add a control message :" + message);
            boolean interrupted = !timer.backoffAndSleep();
            if (interrupted)
                throw new InterruptedException("Got interrupted while waiting to write control Message to EVB : " + message);
        } else {
            _log.info("Sent a control message :" + message);
            success = true;
        }
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) UnifiedClientStats(com.linkedin.databus.client.pub.mbean.UnifiedClientStats) ByteArrayInputStream(java.io.ByteArrayInputStream) BackoffTimerStaticConfig(com.linkedin.databus2.core.BackoffTimerStaticConfig) Checkpoint(com.linkedin.databus.core.Checkpoint) BackoffTimer(com.linkedin.databus2.core.BackoffTimer)

Example 2 with BackoffTimer

use of com.linkedin.databus2.core.BackoffTimer in project databus by linkedin.

the class RelayPullThread method doPickRelay.

protected void doPickRelay(ConnectionState curState) {
    int serversNum = _servers.size();
    if (0 == serversNum) {
        enqueueMessage(LifecycleMessage.createSuspendOnErroMessage(new DatabusException("No relays specified")));
        return;
    }
    Random rng = new Random();
    DatabusRelayConnection relayConn = null;
    ServerInfo serverInfo = null;
    int retriesLeft;
    BackoffTimer originalCounter = _status.getRetriesCounter();
    if (curState.isRelayFellOff())
        _status.setRetriesCounter(_retriesOnFallOff);
    while (null == relayConn && (retriesLeft = _status.getRetriesLeft()) >= 0 && !checkForShutdownRequest()) {
        _log.info("picking a relay; retries left:" + retriesLeft + ", Backoff Timer :" + _status.getRetriesCounter() + ", Are we retrying because of SCNNotFoundException : " + curState.isRelayFellOff());
        backoffOnPullError();
        _curServerIdx = (_curServerIdx < 0) ? rng.nextInt(serversNum) : (_curServerIdx + 1) % serversNum;
        Iterator<ServerInfo> setIter = _servers.iterator();
        for (int i = 0; i <= _curServerIdx; ++i) serverInfo = setIter.next();
        try {
            relayConn = _sourcesConn.getRelayConnFactory().createRelayConnection(serverInfo, this, _remoteExceptionHandler);
            _log.info("picked a relay:" + serverInfo.toSimpleString());
        } catch (Exception e) {
            _log.error("Unable to get connection to relay:" + serverInfo.toSimpleString(), e);
        }
    }
    _status.setRetriesCounter(originalCounter);
    if (!checkForShutdownRequest()) {
        _curServer = serverInfo;
        if (null == relayConn) {
            if (_currentState.isRelayFellOff()) {
                boolean enqueueMessage = false;
                try {
                    enqueueMessage = onRelayFellOff(curState, curState.getCheckpoint(), new ScnNotFoundException("Retries on SCNNotFoundException exhausted !!"));
                } catch (InterruptedException ie) {
                    _log.error("interrupted while processing onRelayFellOff", ie);
                    curState.switchToPickServer();
                    enqueueMessage(curState);
                } catch (InvalidEventException e) {
                    _log.error("error trying to notify dispatcher of bootstrapping :" + e.getMessage(), e);
                    curState.switchToPickServer();
                    enqueueMessage(curState);
                }
                if (enqueueMessage)
                    enqueueMessage(curState);
            } else {
                // There are no retries left. Invoke an onError callback
                try {
                    _log.info("Puller retries exhausted. Injecting an error event on dispatcher queue to invoke onError callback");
                    _remoteExceptionHandler.handleException(new PullerRetriesExhaustedException());
                } catch (InterruptedException ie) {
                    _log.error("Interrupted while processing retries exhausted", ie);
                } catch (InvalidEventException e) {
                    _log.error("Error trying to notify dispatcher of puller retries getting exhausted", e);
                }
                _log.error("Cannot find a relay");
            }
        } else {
            DatabusRelayConnection oldRelayConn = curState.getRelayConnection();
            if (null != oldRelayConn) {
                resetConnectionAndSetFlag();
            }
            sendHeartbeat(_sourcesConn.getUnifiedClientStats());
            _log.info("Relay Puller switching to request sources");
            curState.switchToRequestSources(serverInfo, serverInfo.getAddress(), relayConn);
            _lastOpenConnection = relayConn;
            enqueueMessage(curState);
        }
    }
}
Also used : ServerInfo(com.linkedin.databus.client.pub.ServerInfo) Checkpoint(com.linkedin.databus.core.Checkpoint) InvalidEventException(com.linkedin.databus.core.InvalidEventException) ScnNotFoundException(com.linkedin.databus.core.ScnNotFoundException) PendingEventTooLargeException(com.linkedin.databus.core.PendingEventTooLargeException) BootstrapDatabaseTooOldException(com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException) DatabusException(com.linkedin.databus2.core.DatabusException) PullerRetriesExhaustedException(com.linkedin.databus.core.PullerRetriesExhaustedException) BackoffTimer(com.linkedin.databus2.core.BackoffTimer) DatabusException(com.linkedin.databus2.core.DatabusException) Random(java.util.Random) ScnNotFoundException(com.linkedin.databus.core.ScnNotFoundException) InvalidEventException(com.linkedin.databus.core.InvalidEventException) PullerRetriesExhaustedException(com.linkedin.databus.core.PullerRetriesExhaustedException)

Aggregations

Checkpoint (com.linkedin.databus.core.Checkpoint)2 BackoffTimer (com.linkedin.databus2.core.BackoffTimer)2 ServerInfo (com.linkedin.databus.client.pub.ServerInfo)1 UnifiedClientStats (com.linkedin.databus.client.pub.mbean.UnifiedClientStats)1 InvalidEventException (com.linkedin.databus.core.InvalidEventException)1 PendingEventTooLargeException (com.linkedin.databus.core.PendingEventTooLargeException)1 PullerRetriesExhaustedException (com.linkedin.databus.core.PullerRetriesExhaustedException)1 ScnNotFoundException (com.linkedin.databus.core.ScnNotFoundException)1 BackoffTimerStaticConfig (com.linkedin.databus2.core.BackoffTimerStaticConfig)1 DatabusException (com.linkedin.databus2.core.DatabusException)1 BootstrapDatabaseTooOldException (com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1 Random (java.util.Random)1