Search in sources :

Example 6 with DatabusException

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

the class OpenReplicatorEventProducerServiceProvider method buildEventFactory.

public OpenReplicatorAvroEventFactory buildEventFactory(LogicalSourceStaticConfig sourceConfig, PhysicalSourceStaticConfig pConfig, SchemaRegistryService schemaRegistryService) throws DatabusException, EventCreationException, UnsupportedKeyException, InvalidConfigException {
    String schema = null;
    try {
        schema = schemaRegistryService.fetchLatestSchemaBySourceName(sourceConfig.getName());
    } catch (NoSuchSchemaException e) {
        throw new InvalidConfigException("Unable to load the schema for source (" + sourceConfig.getName() + ").");
    }
    if (schema == null) {
        throw new InvalidConfigException("Unable to load the schema for source (" + sourceConfig.getName() + ").");
    }
    LOG.info("Loading schema for source id " + sourceConfig.getId() + ": " + schema);
    String eventViewSchema;
    String eventView;
    if (sourceConfig.getUri().indexOf('.') != -1) {
        String[] parts = sourceConfig.getUri().split("\\.");
        eventViewSchema = parts[0];
        eventView = parts[1];
    } else {
        eventViewSchema = null;
        eventView = sourceConfig.getUri();
    }
    PartitionFunction partitionFunction = buildPartitionFunction(sourceConfig);
    OpenReplicatorAvroEventFactory factory = createEventFactory(eventViewSchema, eventView, sourceConfig, pConfig, schema, partitionFunction);
    return factory;
}
Also used : PartitionFunction(com.linkedin.databus2.producers.PartitionFunction) NoSuchSchemaException(com.linkedin.databus2.schemas.NoSuchSchemaException) InvalidConfigException(com.linkedin.databus.core.util.InvalidConfigException)

Example 7 with DatabusException

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

the class OpenReplicatorEventProducerServiceProvider method createProducer.

@Override
public EventProducer createProducer(PhysicalSourceStaticConfig physicalSourceConfig, SchemaRegistryService schemaRegistryService, DbusEventBufferAppendable eventBuffer, DbusEventsStatisticsCollector statsCollector, MaxSCNReaderWriter checkpointWriter) throws InvalidConfigException {
    // Parse each one of the logical sources
    List<OpenReplicatorAvroEventFactory> eventFactories = new ArrayList<OpenReplicatorAvroEventFactory>();
    for (LogicalSourceStaticConfig sourceConfig : physicalSourceConfig.getSources()) {
        OpenReplicatorAvroEventFactory factory = null;
        try {
            factory = buildEventFactory(sourceConfig, physicalSourceConfig, schemaRegistryService);
        } catch (Exception ex) {
            LOG.error("Got exception while building monitored sources for config :" + sourceConfig, ex);
            throw new InvalidConfigException(ex);
        }
        eventFactories.add(factory);
    }
    EventProducer producer = null;
    try {
        producer = new OpenReplicatorEventProducer(eventFactories, eventBuffer, checkpointWriter, physicalSourceConfig, statsCollector, null, null, schemaRegistryService, JMX_DOMAIN);
    } catch (DatabusException e) {
        LOG.error("Got databus exception when instantiating Open Replicator event producer for source : " + physicalSourceConfig.getName(), e);
        throw new InvalidConfigException(e);
    }
    return producer;
}
Also used : DatabusException(com.linkedin.databus2.core.DatabusException) ArrayList(java.util.ArrayList) LogicalSourceStaticConfig(com.linkedin.databus2.relay.config.LogicalSourceStaticConfig) InvalidConfigException(com.linkedin.databus.core.util.InvalidConfigException) DatabusException(com.linkedin.databus2.core.DatabusException) InvalidConfigException(com.linkedin.databus.core.util.InvalidConfigException) UnsupportedKeyException(com.linkedin.databus.core.UnsupportedKeyException) NoSuchSchemaException(com.linkedin.databus2.schemas.NoSuchSchemaException)

Example 8 with DatabusException

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

the class RegisterRequestProcessor method getSchemas.

/**
   * Returns list of versioned source (or key?) schemas for the given source name and associates
   * them with the specified ID.
   * TODO:  either add support for key schemas or rename method to getSourceSchemas()
   */
private // IN
void getSchemas(// IN
SchemaRegistryService schemaRegistry, // IN:  source name
String name, // IN
Integer sourceId, // IN (for error-logging only)
String sources, // OUT
ArrayList<RegisterResponseEntry> registeredSources) throws RequestProcessingException {
    Map<Short, String> versionedSchemas = null;
    try {
        versionedSchemas = schemaRegistry.fetchAllSchemaVersionsBySourceName(name);
    } catch (DatabusException ie) {
        HttpStatisticsCollector relayStatsCollector = _relay.getHttpStatisticsCollector();
        if (relayStatsCollector != null)
            relayStatsCollector.registerInvalidRegisterCall();
        throw new RequestProcessingException(ie);
    }
    if ((null == versionedSchemas) || (versionedSchemas.isEmpty())) {
        HttpStatisticsCollector relayStatsCollector = _relay.getHttpStatisticsCollector();
        if (relayStatsCollector != null)
            relayStatsCollector.registerInvalidRegisterCall();
        LOG.error("Problem fetching schema for sourceId " + sourceId + "; sources string = " + sources);
    } else {
        for (Entry<Short, String> e : versionedSchemas.entrySet()) {
            registeredSources.add(new RegisterResponseEntry(sourceId.longValue(), e.getKey(), e.getValue()));
        }
    }
}
Also used : DatabusException(com.linkedin.databus2.core.DatabusException) HttpStatisticsCollector(com.linkedin.databus2.core.container.monitoring.mbean.HttpStatisticsCollector) RegisterResponseEntry(com.linkedin.databus2.core.container.request.RegisterResponseEntry) RequestProcessingException(com.linkedin.databus2.core.container.request.RequestProcessingException)

Example 9 with DatabusException

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

the class RegisterRequestProcessor method getMetadataSchemas.

/**
   * Returns list of versioned metadata schemas.
   * TODO (DDSDBUS-2093):  implement this.
   */
private // IN
void getMetadataSchemas(// IN
SchemaRegistryService schemaRegistry, // OUT
ArrayList<RegisterResponseMetadataEntry> registeredMetadata) throws RequestProcessingException {
    Map<SchemaId, VersionedSchema> versionedSchemas = null;
    try {
        VersionedSchemaSet schemaSet = schemaRegistry.fetchAllMetadataSchemaVersions();
        if (schemaSet != null) {
            versionedSchemas = schemaSet.getAllVersionsWithSchemaId(SchemaRegistryService.DEFAULT_METADATA_SCHEMA_SOURCE);
        }
    } catch (DatabusException ie) {
        HttpStatisticsCollector relayStatsCollector = _relay.getHttpStatisticsCollector();
        if (relayStatsCollector != null)
            relayStatsCollector.registerInvalidRegisterCall();
        throw new RequestProcessingException(ie);
    }
    if (versionedSchemas != null && !versionedSchemas.isEmpty()) {
        for (SchemaId id : versionedSchemas.keySet()) {
            VersionedSchema entry = versionedSchemas.get(id);
            if (entry.getOrigSchemaStr() == null) {
                throw new RequestProcessingException("Null schema string for metadata version " + entry.getVersion());
            }
            registeredMetadata.add(new RegisterResponseMetadataEntry((short) entry.getVersion(), entry.getOrigSchemaStr(), id.getByteArray()));
        }
    }
}
Also used : DatabusException(com.linkedin.databus2.core.DatabusException) RegisterResponseMetadataEntry(com.linkedin.databus2.core.container.request.RegisterResponseMetadataEntry) HttpStatisticsCollector(com.linkedin.databus2.core.container.monitoring.mbean.HttpStatisticsCollector) SchemaId(com.linkedin.databus2.schemas.SchemaId) RequestProcessingException(com.linkedin.databus2.core.container.request.RequestProcessingException) VersionedSchemaSet(com.linkedin.databus2.schemas.VersionedSchemaSet) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema)

Example 10 with DatabusException

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

the class RelayCommandRequestProcessor method process.

@Override
public DatabusRequest process(DatabusRequest request) throws IOException, RequestProcessingException {
    String command = request.getParams().getProperty(DatabusRequest.PATH_PARAM_NAME);
    if (null == command) {
        throw new InvalidRequestParamValueException(COMMAND_NAME, "command", "null");
    }
    String reply = "Command " + command + " completed ";
    LOG.info("got relayCommand = " + command);
    if (command.equals(SAVE_META_STATE_PARAM)) {
        _relay.saveBufferMetaInfo(true);
    } else if (command.equals(SHUTDOWN_RELAY_PARAM)) {
        String msg = "received shutdown curl request from: " + request.getRemoteAddress() + ". Shutting down\n";
        LOG.warn(msg);
        request.getResponseContent().write(ByteBuffer.wrap(msg.getBytes("UTF-8")));
        request.getResponseContent().close();
        _relay.shutdown();
    } else if (command.equals(VALIDATE_RELAY_BUFFER_PARAM)) {
        _relay.validateRelayBuffers();
    } else if (command.equals(DISCONNECT_CLIENTS)) {
        Channel rspChannel = request.getResponseContent().getRawChannel();
        _relay.disconnectDBusClients(rspChannel);
    } else if (command.equals(RUN_GC_PARAM)) {
        Runtime rt = Runtime.getRuntime();
        long mem = rt.freeMemory();
        LOG.info("mem before gc = " + rt.freeMemory() + " out of " + rt.totalMemory());
        long time = System.currentTimeMillis();
        System.gc();
        time = System.currentTimeMillis() - time;
        mem = rt.freeMemory() - mem;
        reply = new String("GC run. Took " + time + " millsecs. Freed " + mem + " bytes out of " + rt.totalMemory());
    } else if (command.startsWith(RESET_RELAY_BUFFER_PARAM)) {
        // We expect the request to be of the format:
        //   resetRelayBuffer/<dbName>/<partitionId>?prevScn=<long>&binlogOffset=<long>
        String[] resetCommands = command.split("/");
        if (resetCommands.length != 3) {
            throw new InvalidRequestParamValueException(COMMAND_NAME, "command", command);
        }
        String dbName = resetCommands[1];
        String dbPart = resetCommands[2];
        long prevScn = request.getRequiredLongParam(PREV_SCN_PARAM);
        long binlogOffset = request.getOptionalLongParam(BINLOG_OFFSET_PARAM, 0L);
        LOG.info("reset command = " + dbName + " part =" + dbPart);
        try {
            _relay.resetBuffer(new PhysicalPartition(Integer.parseInt(dbPart), dbName), prevScn, binlogOffset);
        } catch (BufferNotFoundException e) {
            reply = new String("command " + command + ":" + e.getMessage());
        }
    } else if (command.startsWith(GET_BINLOG_OFFSET_PARAM)) {
        String[] getOfsArgs = command.split("/");
        if (getOfsArgs.length != 2) {
            throw new InvalidRequestParamValueException(GET_BINLOG_OFFSET_PARAM, "Server ID", "");
        }
        int serverId;
        try {
            serverId = Integer.parseInt(getOfsArgs[1]);
            int[] offset = _relay.getBinlogOffset(serverId);
            if (offset.length != 2) {
                reply = "Error getting binlog offset";
            } else {
                reply = new String("RelayLastEvent(" + offset[0] + "," + offset[1] + ")");
            }
        } catch (NumberFormatException e) {
            throw new InvalidRequestParamValueException(GET_BINLOG_OFFSET_PARAM, "Server ID", getOfsArgs[1]);
        } catch (DatabusException e) {
            reply = new String("command " + command + "failed with:" + e.getMessage());
        }
    } else if (command.startsWith(PRINT_RELAY_INFO_PARAM)) {
        try {
            Map<String, String> infoMap = _relay.printInfo();
            reply = makeJsonResponse(infoMap, request);
        } catch (Exception e) {
            reply = new String("command " + command + " failed with:" + e.getMessage());
        }
    } else {
        // invalid command
        reply = new String("command " + command + " is invalid. Valid commands are: " + SAVE_META_STATE_PARAM + "|" + SHUTDOWN_RELAY_PARAM + "|" + VALIDATE_RELAY_BUFFER_PARAM + "|" + RUN_GC_PARAM + "|" + RESET_RELAY_BUFFER_PARAM + "|" + GET_BINLOG_OFFSET_PARAM + "|" + PRINT_RELAY_INFO_PARAM + "|" + DISCONNECT_CLIENTS);
    }
    byte[] responseBytes = new byte[(reply.length() + 2)];
    System.arraycopy(reply.getBytes("UTF-8"), 0, responseBytes, 0, reply.length());
    int idx = reply.length();
    responseBytes[idx] = (byte) '\r';
    responseBytes[idx + 1] = (byte) '\n';
    request.getResponseContent().write(ByteBuffer.wrap(responseBytes));
    return request;
}
Also used : Channel(org.jboss.netty.channel.Channel) InvalidRequestParamValueException(com.linkedin.databus2.core.container.request.InvalidRequestParamValueException) BufferNotFoundException(com.linkedin.databus2.core.BufferNotFoundException) InvalidRequestParamValueException(com.linkedin.databus2.core.container.request.InvalidRequestParamValueException) DatabusException(com.linkedin.databus2.core.DatabusException) BufferNotFoundException(com.linkedin.databus2.core.BufferNotFoundException) IOException(java.io.IOException) RequestProcessingException(com.linkedin.databus2.core.container.request.RequestProcessingException) DatabusException(com.linkedin.databus2.core.DatabusException) Map(java.util.Map) PhysicalPartition(com.linkedin.databus.core.data_model.PhysicalPartition)

Aggregations

DatabusException (com.linkedin.databus2.core.DatabusException)76 Test (org.testng.annotations.Test)21 ArrayList (java.util.ArrayList)19 IOException (java.io.IOException)14 Schema (org.apache.avro.Schema)14 ConditionCheck (com.linkedin.databus2.test.ConditionCheck)13 Logger (org.apache.log4j.Logger)13 InvalidConfigException (com.linkedin.databus.core.util.InvalidConfigException)12 Channel (org.jboss.netty.channel.Channel)12 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)11 PhysicalPartition (com.linkedin.databus.core.data_model.PhysicalPartition)10 UnsupportedKeyException (com.linkedin.databus.core.UnsupportedKeyException)9 VersionedSchema (com.linkedin.databus2.schemas.VersionedSchema)9 InetSocketAddress (java.net.InetSocketAddress)9 SocketAddress (java.net.SocketAddress)9 SQLException (java.sql.SQLException)9 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)9 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)9 EventCreationException (com.linkedin.databus2.producers.EventCreationException)7 PhysicalSourceStaticConfig (com.linkedin.databus2.relay.config.PhysicalSourceStaticConfig)7