Search in sources :

Example 1 with SCNRegressMessage

use of com.linkedin.databus.core.SCNRegressMessage in project databus by linkedin.

the class RelayPullThread method doReadDataEvents.

protected void doReadDataEvents(ConnectionState curState) {
    boolean debugEnabled = _log.isDebugEnabled();
    boolean enqueueMessage = true;
    try {
        ChunkedBodyReadableByteChannel readChannel = curState.getReadChannel();
        Checkpoint cp = curState.getCheckpoint();
        curState.setRelayFellOff(false);
        String remoteErrorName = RemoteExceptionHandler.getExceptionName(readChannel);
        Throwable knownRemoteError = _remoteExceptionHandler.getException(readChannel);
        if (null != knownRemoteError && knownRemoteError instanceof ScnNotFoundException) {
            if (toTearConnAfterHandlingResponse()) {
                tearConnectionAndEnqueuePickServer();
                enqueueMessage = false;
            } else {
                curState.setRelayFellOff(true);
                if (_retriesOnFallOff.getRemainingRetriesNum() > 0) {
                    _log.error("Got SCNNotFoundException. Retry (" + _retriesOnFallOff.getRetriesNum() + ") out of " + _retriesOnFallOff.getConfig().getMaxRetryNum());
                    curState.switchToPickServer();
                } else {
                    enqueueMessage = onRelayFellOff(curState, cp, knownRemoteError);
                }
            }
        } else if (null != remoteErrorName) {
            if (toTearConnAfterHandlingResponse()) {
                tearConnectionAndEnqueuePickServer();
                enqueueMessage = false;
            } else {
                // remote processing error
                _log.error("read events error: " + RemoteExceptionHandler.getExceptionMessage(readChannel));
                curState.switchToStreamResponseError();
            }
        } else {
            /*DispatcherState dispatchState = curState.getDispatcherState();
          dispatchState.switchToDispatchEvents();
          _dispatcherThread.addNewStateBlocking(dispatchState);*/
            if (debugEnabled)
                _log.debug("Sending events to buffer");
            DbusEventsStatisticsCollector connCollector = _sourcesConn.getInboundEventsStatsCollector();
            if (curState.isSCNRegress()) {
                _log.info("SCN Regress requested !! Sending a SCN Regress Message to dispatcher. Curr Ckpt :" + curState.getCheckpoint());
                DbusEvent regressEvent = getEventFactory().createSCNRegressEvent(new SCNRegressMessage(curState.getCheckpoint()));
                writeEventToRelayDispatcher(curState, regressEvent, "SCN Regress Event from ckpt :" + curState.getCheckpoint());
                curState.setSCNRegress(false);
            }
            UnifiedClientStats unifiedClientStats = _sourcesConn.getUnifiedClientStats();
            if (unifiedClientStats != null) {
                // failsafe:  we're definitely not bootstrapping here
                unifiedClientStats.setBootstrappingState(false);
                sendHeartbeat(unifiedClientStats);
            }
            int eventsNum = curState.getDataEventsBuffer().readEvents(readChannel, curState.getListeners(), connCollector);
            boolean resetConnection = false;
            if (eventsNum > 0) {
                _timeSinceEventsSec = System.currentTimeMillis();
                cp.checkPoint();
            } else {
                // check how long it has been since we got some events
                if (_remoteExceptionHandler.getPendingEventSize(readChannel) > curState.getDataEventsBuffer().getMaxReadBufferCapacity()) {
                    // The relay had a pending event that we can never accommodate. This is fatal error.
                    String err = "ReadBuffer max capacity(" + curState.getDataEventsBuffer().getMaxReadBufferCapacity() + ") is less than event size(" + _remoteExceptionHandler.getPendingEventSize(readChannel) + "). Increase databus.client.connectionDefaults.eventBuffer.maxEventSize and restart.";
                    _log.fatal(err);
                    enqueueMessage(LifecycleMessage.createSuspendOnErroMessage(new PendingEventTooLargeException(err)));
                    return;
                } else {
                    if (_noEventsConnectionResetTimeSec > 0) {
                        // unless the feature is disabled (<=0)
                        resetConnection = (System.currentTimeMillis() - _timeSinceEventsSec) / 1000 > _noEventsConnectionResetTimeSec;
                        if (resetConnection) {
                            _timeSinceEventsSec = System.currentTimeMillis();
                            _log.warn("about to reset connection to relay " + curState.getServerInetAddress() + ", because there were no events for " + _noEventsConnectionResetTimeSec + "secs");
                        }
                    }
                }
            }
            if (debugEnabled)
                _log.debug("Events read: " + eventsNum);
            // if it has been too long since we got non-empty responses - the relay may be stuck, try to reconnect
            if (toTearConnAfterHandlingResponse() || resetConnection) {
                tearConnectionAndEnqueuePickServer();
                enqueueMessage = false;
            } else {
                curState.switchToStreamResponseDone();
                resetServerRetries();
            }
        }
        if (enqueueMessage)
            enqueueMessage(curState);
    } catch (InterruptedException ie) {
        _log.warn("interrupted", ie);
        curState.switchToStreamResponseError();
        enqueueMessage(curState);
    } catch (InvalidEventException e) {
        _log.error("error reading events from server:" + e, e);
        curState.switchToStreamResponseError();
        enqueueMessage(curState);
    } catch (RuntimeException e) {
        _log.error("runtime error reading events from server: " + e, e);
        curState.switchToStreamResponseError();
        enqueueMessage(curState);
    }
}
Also used : UnifiedClientStats(com.linkedin.databus.client.pub.mbean.UnifiedClientStats) PendingEventTooLargeException(com.linkedin.databus.core.PendingEventTooLargeException) DbusEvent(com.linkedin.databus.core.DbusEvent) DbusEventsStatisticsCollector(com.linkedin.databus.core.monitoring.mbean.DbusEventsStatisticsCollector) Checkpoint(com.linkedin.databus.core.Checkpoint) ScnNotFoundException(com.linkedin.databus.core.ScnNotFoundException) SCNRegressMessage(com.linkedin.databus.core.SCNRegressMessage) InvalidEventException(com.linkedin.databus.core.InvalidEventException)

Example 2 with SCNRegressMessage

use of com.linkedin.databus.core.SCNRegressMessage in project databus by linkedin.

the class RelayDispatcher method processSysEvent.

@Override
protected boolean processSysEvent(DispatcherState curState, DbusEvent event) {
    boolean success = true;
    if (event.isCheckpointMessage()) {
        Checkpoint ckpt = null;
        try {
            ckpt = DbusEventUtils.getCheckpointFromEvent(event);
            DbusClientMode bootstrapMode = ckpt.getConsumptionMode();
            if (bootstrapMode != DbusClientMode.ONLINE_CONSUMPTION) {
                if (_bootstrapPuller == null) {
                    _log.error("Checkpoint specifies that the consumer is bootstrapping, but bootstrapPuller is not present (Is bootstrap disabled ?)");
                    return false;
                }
                ckpt.setConsumptionMode(DbusClientMode.BOOTSTRAP_SNAPSHOT);
                if (curState.getStateId() != DispatcherState.StateId.EXPECT_EVENT_WINDOW) {
                    _log.warn("The current state of the dispatcher is NOT as expected (" + DispatcherState.StateId.EXPECT_EVENT_WINDOW.name() + "). State prior to this: " + curState.getStateId().name());
                // Fixing bug that caused TestRelayBootstrapSwitch to fail; no apparent need to rollback
                // curState.switchToRollback();
                // doRollback(curState);
                }
                curState.getEventsIterator().getEventBuffer().clear();
                curState.resetIterators();
                curState.switchToExpectEventWindow();
                _bootstrapPuller.enqueueMessage(LifecycleMessage.createStartMessage());
                _log.info("Switching to bootstrap mode");
            } else {
                success = super.processSysEvent(curState, event);
            }
        } catch (Exception e) {
            DbusPrettyLogUtils.logExceptionAtError("Internal error processing a system event", e, _log);
            success = false;
        }
    } else if (event.isSCNRegressMessage()) {
        SCNRegressMessage message = DbusEventUtils.getSCNRegressFromEvent(event);
        _log.info("Switching relays, some of the events maybe replayed. The Checkpoint to which the client with regress: " + message);
        curState.setSCNRegress(true);
        curState.switchToExpectEventWindow();
    // enqueueMessage(curState);
    } else {
        success = super.processSysEvent(curState, event);
    }
    return success;
}
Also used : Checkpoint(com.linkedin.databus.core.Checkpoint) SCNRegressMessage(com.linkedin.databus.core.SCNRegressMessage) DbusClientMode(com.linkedin.databus.core.DbusClientMode)

Aggregations

Checkpoint (com.linkedin.databus.core.Checkpoint)2 SCNRegressMessage (com.linkedin.databus.core.SCNRegressMessage)2 UnifiedClientStats (com.linkedin.databus.client.pub.mbean.UnifiedClientStats)1 DbusClientMode (com.linkedin.databus.core.DbusClientMode)1 DbusEvent (com.linkedin.databus.core.DbusEvent)1 InvalidEventException (com.linkedin.databus.core.InvalidEventException)1 PendingEventTooLargeException (com.linkedin.databus.core.PendingEventTooLargeException)1 ScnNotFoundException (com.linkedin.databus.core.ScnNotFoundException)1 DbusEventsStatisticsCollector (com.linkedin.databus.core.monitoring.mbean.DbusEventsStatisticsCollector)1