Search in sources :

Example 41 with DatabusRequest

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

the class ClientStatsRequestProcessor method processEventsSourceStats.

private void processEventsSourceStats(DbusEventsStatisticsCollector statsCollector, String prefix, DatabusRequest request) throws IOException, RequestProcessingException {
    if (null == statsCollector)
        return;
    String category = request.getParams().getProperty(DatabusRequest.PATH_PARAM_NAME);
    String sourceIdStr = category.substring(prefix.length());
    int sourceId = -1;
    try {
        sourceId = Integer.valueOf(sourceIdStr);
    } catch (NumberFormatException nfe) {
        throw new InvalidRequestParamValueException(request.getName(), prefix, sourceIdStr);
    }
    DbusEventsTotalStats sourceStats = statsCollector.getSourceStats(sourceId);
    if (null == sourceStats) {
        throw new InvalidRequestParamValueException(request.getName(), prefix, sourceIdStr);
    }
    writeJsonObjectToResponse(sourceStats, request);
    if (request.getRequestType() == HttpMethod.PUT || request.getRequestType() == HttpMethod.POST) {
        enableOrResetStatsMBean(sourceStats, request);
    }
}
Also used : DbusEventsTotalStats(com.linkedin.databus.core.monitoring.mbean.DbusEventsTotalStats) InvalidRequestParamValueException(com.linkedin.databus2.core.container.request.InvalidRequestParamValueException)

Example 42 with DatabusRequest

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

the class ClientStatsRequestProcessor method processOutboundHttpSourceStats.

private void processOutboundHttpSourceStats(DatabusRequest request) throws IOException, RequestProcessingException {
    String category = request.getParams().getProperty(DatabusRequest.PATH_PARAM_NAME);
    String sourceIdStr = category.substring(INBOUND_HTTP_SOURCE_PREFIX.length());
    int sourceId = -1;
    try {
        sourceId = Integer.valueOf(sourceIdStr);
    } catch (NumberFormatException nfe) {
        throw new InvalidRequestParamValueException(request.getName(), INBOUND_HTTP_SOURCE_PREFIX, sourceIdStr);
    }
    DbusHttpTotalStats sourceStats = _client.getHttpStatsCollector().getSourceStats(sourceId);
    if (null == sourceStats) {
        throw new InvalidRequestParamValueException(request.getName(), INBOUND_HTTP_SOURCE_PREFIX, sourceIdStr);
    }
    writeJsonObjectToResponse(sourceStats, request);
    if (request.getRequestType() == HttpMethod.PUT || request.getRequestType() == HttpMethod.POST) {
        enableOrResetStatsMBean(sourceStats, request);
    }
}
Also used : DbusHttpTotalStats(com.linkedin.databus2.core.container.monitoring.mbean.DbusHttpTotalStats) InvalidRequestParamValueException(com.linkedin.databus2.core.container.request.InvalidRequestParamValueException)

Example 43 with DatabusRequest

use of com.linkedin.databus2.core.container.request.DatabusRequest 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 44 with DatabusRequest

use of com.linkedin.databus2.core.container.request.DatabusRequest 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)

Example 45 with DatabusRequest

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

the class TestDatabusRequest method makeRequest.

private DatabusRequest makeRequest() throws Exception {
    SocketAddress sockAddr = new InetSocketAddress(InetAddress.getByAddress(ipAddr), port);
    Properties props = new Properties();
    props.setProperty(prop1, propval1);
    props.setProperty(prop2, propval2);
    ServerContainer.ExecutorConfig defaultConfig = new ServerContainer.ExecutorConfig(3, 2, 100, false, 5);
    ServerContainer.ExecutorConfig ioConfig = new ServerContainer.ExecutorConfig(5, 4, 50, true, 6);
    ServerContainer.RuntimeConfig runtimeConfig = new ServerContainer.RuntimeConfig(200, defaultConfig, ioConfig);
    return new DatabusRequest(reqName, method, sockAddr, props, runtimeConfig);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Properties(java.util.Properties) ServerContainer(com.linkedin.databus2.core.container.netty.ServerContainer)

Aggregations

InvalidRequestParamValueException (com.linkedin.databus2.core.container.request.InvalidRequestParamValueException)20 RequestProcessingException (com.linkedin.databus2.core.container.request.RequestProcessingException)17 ArrayList (java.util.ArrayList)9 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)9 LogicalSource (com.linkedin.databus.core.data_model.LogicalSource)8 DatabusRequest (com.linkedin.databus2.core.container.request.DatabusRequest)8 IOException (java.io.IOException)8 Properties (java.util.Properties)8 DbusHttpTotalStats (com.linkedin.databus2.core.container.monitoring.mbean.DbusHttpTotalStats)6 List (java.util.List)6 RegistrationId (com.linkedin.databus.client.pub.RegistrationId)5 RegisterRequestProcessor (com.linkedin.databus.container.request.RegisterRequestProcessor)5 PhysicalPartition (com.linkedin.databus.core.data_model.PhysicalPartition)5 ChunkedWritableByteChannel (com.linkedin.databus2.core.container.ChunkedWritableByteChannel)5 EventProducer (com.linkedin.databus2.producers.EventProducer)5 SchemaRegistryService (com.linkedin.databus2.schemas.SchemaRegistryService)5 SourceIdNameRegistry (com.linkedin.databus2.schemas.SourceIdNameRegistry)5 HashMap (java.util.HashMap)5 DatabusRegistration (com.linkedin.databus.client.pub.DatabusRegistration)4 DatabusV3Registration (com.linkedin.databus.client.pub.DatabusV3Registration)4