Search in sources :

Example 6 with BootstrapDatabaseTooOldException

use of com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException in project databus by linkedin.

the class RemoteExceptionHandler method suspendConnectionOnError.

private void suspendConnectionOnError(Throwable exception) throws InvalidEventException, InterruptedException {
    // suspend pull threads
    _sourcesConn.getConnectionStatus().suspendOnError(exception);
    // send an error event to dispatcher through dbusEventBuffer
    DbusEventInternalReadable errorEvent = null;
    if (exception instanceof BootstrapDatabaseTooOldException) {
        errorEvent = _eventFactory.createErrorEvent(new DbusErrorEvent(exception, DbusEventInternalWritable.BOOTSTRAPTOOOLD_ERROR_SRCID));
    } else if (exception instanceof PullerRetriesExhaustedException) {
        errorEvent = _eventFactory.createErrorEvent(new DbusErrorEvent(exception, DbusEventInternalWritable.PULLER_RETRIES_EXPIRED));
    } else {
        throw new InvalidEventException("Got an unrecognizable exception ");
    }
    byte[] errorEventBytes = new byte[errorEvent.getRawBytes().limit()];
    if (LOG.isDebugEnabled()) {
        LOG.debug("error event size: " + errorEventBytes.length);
        LOG.debug("error event:" + errorEvent.toString());
    }
    errorEvent.getRawBytes().get(errorEventBytes);
    ByteArrayInputStream errIs = new ByteArrayInputStream(errorEventBytes);
    ReadableByteChannel errRbc = Channels.newChannel(errIs);
    boolean success = false;
    int retryCounter = 0;
    while (!success && retryCounter < 10) {
        String errMsg = "Sending an internal system event to dispatcher. Retry count = " + retryCounter;
        DbusPrettyLogUtils.logExceptionAtInfo(errMsg, exception, LOG);
        success = _dbusEventBuffer.readEvents(errRbc) > 0 ? true : false;
        if (!success) {
            LOG.warn("Unable to send an internal system event to dispatcher. Will retry later " + retryCounter);
            retryCounter++;
            Thread.sleep(1000);
        }
    }
}
Also used : BootstrapDatabaseTooOldException(com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException) ReadableByteChannel(java.nio.channels.ReadableByteChannel) ChunkedBodyReadableByteChannel(com.linkedin.databus.client.ChunkedBodyReadableByteChannel) ByteArrayInputStream(java.io.ByteArrayInputStream) DbusEventInternalReadable(com.linkedin.databus.core.DbusEventInternalReadable) DbusErrorEvent(com.linkedin.databus.core.DbusErrorEvent) InvalidEventException(com.linkedin.databus.core.InvalidEventException) PullerRetriesExhaustedException(com.linkedin.databus.core.PullerRetriesExhaustedException)

Example 7 with BootstrapDatabaseTooOldException

use of com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException in project databus by linkedin.

the class RemoteExceptionHandler method getException.

public Throwable getException(ChunkedBodyReadableByteChannel readChannel) {
    Throwable remoteException = null;
    String err = getExceptionName(readChannel);
    if (null != err) {
        // in theory, we shall be reading the actual exception from the read channel.
        if (err.equalsIgnoreCase(ScnNotFoundException.class.getName())) {
            remoteException = new ScnNotFoundException();
        } else if (err.equalsIgnoreCase(BootstrapDatabaseTooOldException.class.getName())) {
            remoteException = new BootstrapDatabaseTooOldException();
        } else if (err.equalsIgnoreCase(PullerRetriesExhaustedException.class.getName())) {
            remoteException = new PullerRetriesExhaustedException();
        } else if (err.equalsIgnoreCase(BootstrapDatabaseTooYoungException.class.getName())) {
            remoteException = new BootstrapDatabaseTooYoungException();
        } else if (err.equalsIgnoreCase(BootstrapDBException.class.getName())) {
            remoteException = new BootstrapDBException();
        } else if (err.equalsIgnoreCase(SQLException.class.getName())) {
            remoteException = new SQLException();
        } else {
            LOG.error("Unexpected remote error received: " + err);
        }
        LOG.info("Remote exception received: " + remoteException);
    }
    return remoteException;
}
Also used : BootstrapDatabaseTooOldException(com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException) SQLException(java.sql.SQLException) ScnNotFoundException(com.linkedin.databus.core.ScnNotFoundException) BootstrapDBException(com.linkedin.databus2.core.container.request.BootstrapDBException) BootstrapDatabaseTooYoungException(com.linkedin.databus2.core.container.request.BootstrapDatabaseTooYoungException) PullerRetriesExhaustedException(com.linkedin.databus.core.PullerRetriesExhaustedException)

Example 8 with BootstrapDatabaseTooOldException

use of com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException in project databus by linkedin.

the class RelayPullThread method onRelayFellOff.

private boolean onRelayFellOff(ConnectionState curState, Checkpoint cp, Throwable knownRemoteError) throws InvalidEventException, InterruptedException {
    boolean enqueueMessage = true;
    _log.error("Retries on SCNNotFoundException exhausted !!");
    _retriesOnFallOff.reset();
    //need to bootstrap, suspend or read From Latest SCN
    if (!_sourcesConn.isBootstrapEnabled()) {
        _log.error("No scn found on relay while no bootstrap services provided:");
        _log.error(" bootstrapServices=" + _sourcesConn.getBootstrapServices() + "; bootstrapRegistrations=" + _sourcesConn.getBootstrapRegistrations());
        if (_isReadLatestScnOnErrorEnabled) {
            // Read From Latest SCN on Error
            _log.error("Read Latest SCN Window on SCNNotFoundException is enabled. Will start reading from the lastest SCN Window !!");
            curState.getRelayConnection().enableReadFromLatestScn(true);
            _currentState.setRelayFellOff(false);
            curState.switchToStreamResponseDone();
        } else {
            _log.fatal("Got SCNNotFoundException but Read Latest SCN Window and bootstrap are disabled !!");
            _remoteExceptionHandler.handleException(new BootstrapDatabaseTooOldException(knownRemoteError));
            enqueueMessage = false;
        }
    } else {
        _log.info("Requested scn " + cp.getWindowScn() + " not found on relay; switching to bootstrap service");
        curState.switchToBootstrap(cp);
        UnifiedClientStats unifiedClientStats = _sourcesConn.getUnifiedClientStats();
        if (unifiedClientStats != null) {
            unifiedClientStats.setBootstrappingState(true);
        }
    }
    return enqueueMessage;
}
Also used : BootstrapDatabaseTooOldException(com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException) UnifiedClientStats(com.linkedin.databus.client.pub.mbean.UnifiedClientStats)

Example 9 with BootstrapDatabaseTooOldException

use of com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException in project databus by linkedin.

the class TargetSCNRequestProcessor method doProcess.

@Override
protected DatabusRequest doProcess(DatabusRequest request) throws IOException, RequestProcessingException {
    BootstrapHttpStatsCollector bootstrapStatsCollector = _bootstrapServer.getBootstrapStatsCollector();
    long startTime = System.currentTimeMillis();
    int srcId = -1;
    long targetScn = -1;
    String source = request.getRequiredStringParam(SOURCE_PARAM);
    BootstrapSCNProcessor processor = null;
    try {
        processor = new BootstrapSCNProcessor(_config, _bootstrapServer.getInboundEventStatisticsCollector());
        try {
            // get src id from db
            BootstrapDBMetaDataDAO.SourceStatusInfo srcIdStatus = processor.getSrcIdStatusFromDB(source, true);
            if (!srcIdStatus.isValidSource())
                throw new BootstrapProcessingException("Bootstrap DB not servicing source :" + source);
            srcId = srcIdStatus.getSrcId();
            // select target scn
            targetScn = processor.getSourceTargetScn(srcId);
        } catch (BootstrapDatabaseTooOldException tooOldException) {
            if (bootstrapStatsCollector != null) {
                bootstrapStatsCollector.registerErrTargetSCN();
                bootstrapStatsCollector.registerErrDatabaseTooOld();
            }
            LOG.error("The bootstrap database is too old!", tooOldException);
            throw new RequestProcessingException(tooOldException);
        } catch (SQLException e) {
            if (bootstrapStatsCollector != null) {
                bootstrapStatsCollector.registerErrTargetSCN();
                bootstrapStatsCollector.registerErrSqlException();
            }
            LOG.error("Error encountered while fetching targetSCN from database.", e);
            throw new RequestProcessingException(e);
        }
        ObjectMapper mapper = new ObjectMapper();
        StringWriter out = new StringWriter(1024);
        mapper.writeValue(out, String.valueOf(targetScn));
        byte[] resultBytes = out.toString().getBytes(Charset.defaultCharset());
        request.getResponseContent().write(ByteBuffer.wrap(resultBytes));
        LOG.info("targetSCN: " + targetScn);
    } catch (Exception ex) {
        LOG.error("Got exception while calculating targetSCN", ex);
        throw new RequestProcessingException(ex);
    } finally {
        if (null != processor)
            processor.shutdown();
    }
    if (bootstrapStatsCollector != null) {
        bootstrapStatsCollector.registerTargetSCNReq(System.currentTimeMillis() - startTime);
    }
    return request;
}
Also used : BootstrapHttpStatsCollector(com.linkedin.databus.bootstrap.common.BootstrapHttpStatsCollector) SQLException(java.sql.SQLException) BootstrapProcessingException(com.linkedin.databus.bootstrap.api.BootstrapProcessingException) BootstrapDBMetaDataDAO(com.linkedin.databus.bootstrap.common.BootstrapDBMetaDataDAO) BootstrapProcessingException(com.linkedin.databus.bootstrap.api.BootstrapProcessingException) IOException(java.io.IOException) BootstrapDatabaseTooOldException(com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException) SQLException(java.sql.SQLException) RequestProcessingException(com.linkedin.databus2.core.container.request.RequestProcessingException) BootstrapDatabaseTooOldException(com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException) StringWriter(java.io.StringWriter) RequestProcessingException(com.linkedin.databus2.core.container.request.RequestProcessingException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 10 with BootstrapDatabaseTooOldException

use of com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException in project databus by linkedin.

the class BootstrapRequestProcessor method doProcess.

/*
   * (non-Javadoc)
   *
   * @see
   * com.linkedin.databus.container.request.RequestProcessor#process(com.linkedin.databus
   * .container.request.DatabusRequest)
   */
@Override
protected DatabusRequest doProcess(DatabusRequest request) throws IOException, RequestProcessingException {
    BootstrapProcessor processor = null;
    BootstrapHttpStatsCollector bootstrapStatsCollector = _bootstrapServer.getBootstrapStatsCollector();
    long startTime = System.currentTimeMillis();
    boolean isDebug = LOG.isDebugEnabled();
    try {
        try {
            String threadName = Thread.currentThread().getName();
            DbusEventsStatisticsCollector threadCollector = _bootstrapServer.getOutBoundStatsCollectors().getStatsCollector(threadName);
            if (null == threadCollector) {
                threadCollector = new DbusEventsStatisticsCollector(_bootstrapServer.getContainerStaticConfig().getId(), threadName, true, false, _bootstrapServer.getMbeanServer());
                StatsCollectors<DbusEventsStatisticsCollector> ds = _bootstrapServer.getOutBoundStatsCollectors();
                ds.addStatsCollector(threadName, threadCollector);
            }
            processor = new BootstrapProcessor(_config, threadCollector);
        } catch (Exception e) {
            if (null != bootstrapStatsCollector) {
                bootstrapStatsCollector.registerErrBootstrap();
            }
            throw new RequestProcessingException(e);
        }
        DatabusComponentStatus componentStatus = _componentStatus.getStatusSnapshot();
        if (!componentStatus.isRunningStatus()) {
            if (null != bootstrapStatsCollector)
                bootstrapStatsCollector.registerErrBootstrap();
            throw new RequestProcessingException(componentStatus.getMessage());
        }
        String partitionInfoString = request.getParams().getProperty(PARTITION_INFO_PARAM);
        DbusKeyFilter keyFilter = null;
        if ((null != partitionInfoString) && (!partitionInfoString.isEmpty())) {
            try {
                keyFilter = KeyFilterConfigJSONFactory.parseDbusKeyFilter(partitionInfoString);
                if (isDebug)
                    LOG.debug("ServerSideFilter is :" + keyFilter);
            } catch (Exception ex) {
                String msg = "Unable to parse partitionInfo from request. PartitionInfo was :" + partitionInfoString;
                LOG.error(msg, ex);
                throw new RequestProcessingException(msg, ex);
            }
        }
        String outputFormat = request.getParams().getProperty(OUTPUT_PARAM);
        Encoding enc = Encoding.BINARY;
        if (null != outputFormat) {
            try {
                enc = Encoding.valueOf(outputFormat.toUpperCase());
            } catch (Exception ex) {
                LOG.error("Unable to find the output format for bootstrap request for " + outputFormat + ". Using Binary!!", ex);
            }
        }
        processor.setKeyFilter(keyFilter);
        String checkpointString = request.getRequiredStringParam(CHECKPOINT_PARAM);
        int bufferMarginSpace = DEFAULT_BUFFER_MARGIN_SPACE;
        if (null != _serverHostPort) {
            bufferMarginSpace = Math.max(bufferMarginSpace, (_serverHostPort.length() + Checkpoint.BOOTSTRAP_SERVER_INFO.length() + DEFAULT_JSON_OVERHEAD_BYTES));
        }
        int clientFreeBufferSize = request.getRequiredIntParam(BATCHSIZE_PARAM) - checkpointString.length() - bufferMarginSpace;
        BootstrapEventWriter writer = null;
        if (_config.getPredicatePushDown())
            writer = createEventWriter(request, clientFreeBufferSize, null, enc);
        else
            writer = createEventWriter(request, clientFreeBufferSize, keyFilter, enc);
        Checkpoint cp = new Checkpoint(checkpointString);
        DbusClientMode consumptionMode = cp.getConsumptionMode();
        LOG.info("Bootstrap request received: " + "fetchSize=" + clientFreeBufferSize + ", consumptionMode=" + consumptionMode + ", checkpoint=" + checkpointString + ", predicatePushDown=" + _config.getPredicatePushDown());
        try {
            boolean phaseCompleted = false;
            switch(consumptionMode) {
                case BOOTSTRAP_SNAPSHOT:
                    phaseCompleted = processor.streamSnapShotRows(new Checkpoint(checkpointString), writer);
                    break;
                case BOOTSTRAP_CATCHUP:
                    phaseCompleted = processor.streamCatchupRows(new Checkpoint(checkpointString), writer);
                    break;
                default:
                    if (null != bootstrapStatsCollector)
                        bootstrapStatsCollector.registerErrBootstrap();
                    throw new RequestProcessingException("Unexpected mode: " + consumptionMode);
            }
            if (null != bootstrapStatsCollector)
                bootstrapStatsCollector.registerBootStrapReq(cp, System.currentTimeMillis() - startTime, clientFreeBufferSize);
            if (writer.getNumRowsWritten() == 0 && writer.getSizeOfPendingEvent() > 0) {
                // Append a header to indicate to the client that we do have at least one event to
                // send, but it is too large to fit into client's offered buffer.
                request.getResponseContent().addMetadata(DatabusHttpHeaders.DATABUS_PENDING_EVENT_SIZE, writer.getSizeOfPendingEvent());
                if (isDebug) {
                    LOG.debug("Returning 0 events but have pending event of size " + writer.getSizeOfPendingEvent());
                }
            }
            if (phaseCompleted) {
                request.getResponseContent().setMetadata(BootstrapProcessor.PHASE_COMPLETED_HEADER_NAME, BootstrapProcessor.PHASE_COMPLETED_HEADER_TRUE);
            }
        } catch (BootstrapDatabaseTooOldException e) {
            if (null != bootstrapStatsCollector)
                bootstrapStatsCollector.registerErrDatabaseTooOld();
            LOG.error("Bootstrap database is too old!", e);
            throw new RequestProcessingException(e);
        } catch (BootstrapDBException e) {
            if (null != bootstrapStatsCollector)
                bootstrapStatsCollector.registerErrBootstrap();
            throw new RequestProcessingException(e);
        } catch (SQLException e) {
            if (null != bootstrapStatsCollector)
                bootstrapStatsCollector.registerErrSqlException();
            throw new RequestProcessingException(e);
        } catch (BootstrapProcessingException e) {
            if (null != bootstrapStatsCollector)
                bootstrapStatsCollector.registerErrBootstrap();
            throw new RequestProcessingException(e);
        }
    } finally {
        if (null != processor)
            processor.shutdown();
    }
    return request;
}
Also used : BootstrapHttpStatsCollector(com.linkedin.databus.bootstrap.common.BootstrapHttpStatsCollector) DatabusComponentStatus(com.linkedin.databus.core.DatabusComponentStatus) SQLException(java.sql.SQLException) BootstrapProcessingException(com.linkedin.databus.bootstrap.api.BootstrapProcessingException) DbusClientMode(com.linkedin.databus.core.DbusClientMode) DbusEventsStatisticsCollector(com.linkedin.databus.core.monitoring.mbean.DbusEventsStatisticsCollector) Encoding(com.linkedin.databus.core.Encoding) BootstrapProcessingException(com.linkedin.databus.bootstrap.api.BootstrapProcessingException) BootstrapDBException(com.linkedin.databus2.core.container.request.BootstrapDBException) IOException(java.io.IOException) BootstrapDatabaseTooOldException(com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException) SQLException(java.sql.SQLException) RequestProcessingException(com.linkedin.databus2.core.container.request.RequestProcessingException) DbusKeyFilter(com.linkedin.databus2.core.filter.DbusKeyFilter) Checkpoint(com.linkedin.databus.core.Checkpoint) BootstrapDatabaseTooOldException(com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException) Checkpoint(com.linkedin.databus.core.Checkpoint) RequestProcessingException(com.linkedin.databus2.core.container.request.RequestProcessingException) BootstrapDBException(com.linkedin.databus2.core.container.request.BootstrapDBException)

Aggregations

BootstrapDatabaseTooOldException (com.linkedin.databus2.core.container.request.BootstrapDatabaseTooOldException)11 SQLException (java.sql.SQLException)7 Checkpoint (com.linkedin.databus.core.Checkpoint)6 BootstrapProcessingException (com.linkedin.databus.bootstrap.api.BootstrapProcessingException)4 BootstrapDBMetaDataDAO (com.linkedin.databus.bootstrap.common.BootstrapDBMetaDataDAO)3 BootstrapHttpStatsCollector (com.linkedin.databus.bootstrap.common.BootstrapHttpStatsCollector)3 BootstrapDatabaseTooYoungException (com.linkedin.databus2.core.container.request.BootstrapDatabaseTooYoungException)3 RequestProcessingException (com.linkedin.databus2.core.container.request.RequestProcessingException)3 IOException (java.io.IOException)3 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)3 SourceStatusInfo (com.linkedin.databus.bootstrap.common.BootstrapDBMetaDataDAO.SourceStatusInfo)2 BootstrapDBTimedQuery (com.linkedin.databus.bootstrap.common.BootstrapDBTimedQuery)2 UnifiedClientStats (com.linkedin.databus.client.pub.mbean.UnifiedClientStats)2 DbusEventInternalReadable (com.linkedin.databus.core.DbusEventInternalReadable)2 InvalidEventException (com.linkedin.databus.core.InvalidEventException)2 PullerRetriesExhaustedException (com.linkedin.databus.core.PullerRetriesExhaustedException)2 BootstrapDBException (com.linkedin.databus2.core.container.request.BootstrapDBException)2