Search in sources :

Example 1 with SchemaException

use of org.apache.pulsar.broker.service.schema.exceptions.SchemaException in project pulsar by yahoo.

the class TopicsBase method addSchema.

// Add a new schema to schema registry for a topic
private CompletableFuture<SchemaVersion> addSchema(SchemaData schemaData) {
    // Only need to add to first partition the broker owns since the schema id in schema registry are
    // same for all partitions which is the partitionedTopicName
    List<Integer> partitions = pulsar().getBrokerService().getOwningTopics().get(topicName.getPartitionedTopicName()).values();
    CompletableFuture<SchemaVersion> result = new CompletableFuture<>();
    for (int index = 0; index < partitions.size(); index++) {
        CompletableFuture<SchemaVersion> future = new CompletableFuture<>();
        String topicPartitionName = topicName.getPartition(partitions.get(index)).toString();
        pulsar().getBrokerService().getTopic(topicPartitionName, false).thenAccept(topic -> {
            if (!topic.isPresent()) {
                future.completeExceptionally(new BrokerServiceException.TopicNotFoundException("Topic " + topicPartitionName + " not found"));
            } else {
                topic.get().addSchema(schemaData).thenAccept(schemaVersion -> future.complete(schemaVersion)).exceptionally(exception -> {
                    future.completeExceptionally(exception);
                    return null;
                });
            }
        });
        try {
            result.complete(future.get());
            break;
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug("Fail to add schema to topic " + topicName.getPartitionedTopicName() + " for partition " + partitions.get(index) + " for REST produce request.");
            }
        }
    }
    // Not able to add schema to any partition
    if (!result.isDone()) {
        result.completeExceptionally(new SchemaException("Unable to add schema " + schemaData + " to topic " + topicName.getPartitionedTopicName()));
    }
    return result;
}
Also used : PersistentTopicsBase(org.apache.pulsar.broker.admin.impl.PersistentTopicsBase) Arrays(java.util.Arrays) KeyValueSchemaImpl(org.apache.pulsar.client.impl.schema.KeyValueSchemaImpl) Time(java.sql.Time) URL(java.net.URL) ObjectMapperFactory(org.apache.pulsar.common.util.ObjectMapperFactory) SchemaInfoImpl(org.apache.pulsar.client.impl.schema.SchemaInfoImpl) ByteBuffer(java.nio.ByteBuffer) Decoder(org.apache.avro.io.Decoder) ProducerAck(org.apache.pulsar.websocket.data.ProducerAck) MessageImpl(org.apache.pulsar.client.impl.MessageImpl) Pair(org.apache.commons.lang3.tuple.Pair) LocalTime(java.time.LocalTime) RestException(org.apache.pulsar.broker.web.RestException) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) DateFormat(java.text.DateFormat) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) CompressionType(org.apache.pulsar.client.api.CompressionType) Timestamp(java.sql.Timestamp) AsyncResponse(javax.ws.rs.container.AsyncResponse) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) DatumReader(org.apache.avro.io.DatumReader) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) MessageIdImpl(org.apache.pulsar.client.impl.MessageIdImpl) ProducerAcks(org.apache.pulsar.websocket.data.ProducerAcks) Base64(java.util.Base64) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) FutureUtil(org.apache.pulsar.common.util.FutureUtil) ConcurrentOpenHashSet(org.apache.pulsar.common.util.collections.ConcurrentOpenHashSet) LookupOptions(org.apache.pulsar.broker.namespace.LookupOptions) MessageMetadata(org.apache.pulsar.common.api.proto.MessageMetadata) Response(javax.ws.rs.core.Response) LocalDate(java.time.LocalDate) LongSchemaVersion(org.apache.pulsar.common.schema.LongSchemaVersion) Optional(java.util.Optional) SchemaInfo(org.apache.pulsar.common.schema.SchemaInfo) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) GenericAvroWriter(org.apache.pulsar.client.impl.schema.generic.GenericAvroWriter) TopicName(org.apache.pulsar.common.naming.TopicName) SchemaException(org.apache.pulsar.broker.service.schema.exceptions.SchemaException) ProducerMessage(org.apache.pulsar.websocket.data.ProducerMessage) LocalDateTime(java.time.LocalDateTime) CompletableFuture(java.util.concurrent.CompletableFuture) Message(org.apache.pulsar.client.api.Message) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) GenericJsonWriter(org.apache.pulsar.client.impl.schema.generic.GenericJsonWriter) SchemaType(org.apache.pulsar.common.schema.SchemaType) ProducerMessages(org.apache.pulsar.websocket.data.ProducerMessages) ArrayList(java.util.ArrayList) GenericData(org.apache.avro.generic.GenericData) Commands(org.apache.pulsar.common.protocol.Commands) ByteBuf(io.netty.buffer.ByteBuf) KeyValueEncodingType(org.apache.pulsar.common.schema.KeyValueEncodingType) CompressionCodecProvider(org.apache.pulsar.common.compression.CompressionCodecProvider) Status(javax.ws.rs.core.Response.Status) SchemaRegistry(org.apache.pulsar.broker.service.schema.SchemaRegistry) StringSchema(org.apache.pulsar.client.impl.schema.StringSchema) GenericRecord(org.apache.avro.generic.GenericRecord) GenericAvroRecord(org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord) GenericJsonRecord(org.apache.pulsar.client.impl.schema.generic.GenericJsonRecord) BrokerServiceException(org.apache.pulsar.broker.service.BrokerServiceException) IOException(java.io.IOException) SchemaVersion(org.apache.pulsar.common.protocol.schema.SchemaVersion) Schema(org.apache.pulsar.client.api.Schema) KeyValueSchemaInfo(org.apache.pulsar.client.impl.schema.KeyValueSchemaInfo) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) LookupResult(org.apache.pulsar.broker.lookup.LookupResult) MessageId(org.apache.pulsar.client.api.MessageId) SchemaData(org.apache.pulsar.common.protocol.schema.SchemaData) Collections(java.util.Collections) DecoderFactory(org.apache.avro.io.DecoderFactory) AvroBaseStructSchema(org.apache.pulsar.client.impl.schema.AvroBaseStructSchema) SchemaException(org.apache.pulsar.broker.service.schema.exceptions.SchemaException) CompletableFuture(java.util.concurrent.CompletableFuture) LongSchemaVersion(org.apache.pulsar.common.schema.LongSchemaVersion) SchemaVersion(org.apache.pulsar.common.protocol.schema.SchemaVersion) BrokerServiceException(org.apache.pulsar.broker.service.BrokerServiceException) RestException(org.apache.pulsar.broker.web.RestException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) SchemaException(org.apache.pulsar.broker.service.schema.exceptions.SchemaException) BrokerServiceException(org.apache.pulsar.broker.service.BrokerServiceException) IOException(java.io.IOException)

Example 2 with SchemaException

use of org.apache.pulsar.broker.service.schema.exceptions.SchemaException in project incubator-pulsar by apache.

the class TopicsBase method addSchema.

// Add a new schema to schema registry for a topic
private CompletableFuture<SchemaVersion> addSchema(SchemaData schemaData) {
    // Only need to add to first partition the broker owns since the schema id in schema registry are
    // same for all partitions which is the partitionedTopicName
    List<Integer> partitions = pulsar().getBrokerService().getOwningTopics().get(topicName.getPartitionedTopicName()).values();
    CompletableFuture<SchemaVersion> result = new CompletableFuture<>();
    for (int index = 0; index < partitions.size(); index++) {
        CompletableFuture<SchemaVersion> future = new CompletableFuture<>();
        String topicPartitionName = topicName.getPartition(partitions.get(index)).toString();
        pulsar().getBrokerService().getTopic(topicPartitionName, false).thenAccept(topic -> {
            if (!topic.isPresent()) {
                future.completeExceptionally(new BrokerServiceException.TopicNotFoundException("Topic " + topicPartitionName + " not found"));
            } else {
                topic.get().addSchema(schemaData).thenAccept(schemaVersion -> future.complete(schemaVersion)).exceptionally(exception -> {
                    future.completeExceptionally(exception);
                    return null;
                });
            }
        });
        try {
            result.complete(future.get());
            break;
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug("Fail to add schema to topic " + topicName.getPartitionedTopicName() + " for partition " + partitions.get(index) + " for REST produce request.");
            }
        }
    }
    // Not able to add schema to any partition
    if (!result.isDone()) {
        result.completeExceptionally(new SchemaException("Unable to add schema " + schemaData + " to topic " + topicName.getPartitionedTopicName()));
    }
    return result;
}
Also used : PersistentTopicsBase(org.apache.pulsar.broker.admin.impl.PersistentTopicsBase) Arrays(java.util.Arrays) KeyValueSchemaImpl(org.apache.pulsar.client.impl.schema.KeyValueSchemaImpl) Time(java.sql.Time) URL(java.net.URL) ObjectMapperFactory(org.apache.pulsar.common.util.ObjectMapperFactory) SchemaInfoImpl(org.apache.pulsar.client.impl.schema.SchemaInfoImpl) ByteBuffer(java.nio.ByteBuffer) Decoder(org.apache.avro.io.Decoder) ProducerAck(org.apache.pulsar.websocket.data.ProducerAck) MessageImpl(org.apache.pulsar.client.impl.MessageImpl) Pair(org.apache.commons.lang3.tuple.Pair) LocalTime(java.time.LocalTime) RestException(org.apache.pulsar.broker.web.RestException) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) DateFormat(java.text.DateFormat) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) CompressionType(org.apache.pulsar.client.api.CompressionType) Timestamp(java.sql.Timestamp) AsyncResponse(javax.ws.rs.container.AsyncResponse) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) DatumReader(org.apache.avro.io.DatumReader) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) MessageIdImpl(org.apache.pulsar.client.impl.MessageIdImpl) ProducerAcks(org.apache.pulsar.websocket.data.ProducerAcks) Base64(java.util.Base64) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) FutureUtil(org.apache.pulsar.common.util.FutureUtil) ConcurrentOpenHashSet(org.apache.pulsar.common.util.collections.ConcurrentOpenHashSet) LookupOptions(org.apache.pulsar.broker.namespace.LookupOptions) MessageMetadata(org.apache.pulsar.common.api.proto.MessageMetadata) Response(javax.ws.rs.core.Response) LocalDate(java.time.LocalDate) LongSchemaVersion(org.apache.pulsar.common.schema.LongSchemaVersion) Optional(java.util.Optional) SchemaInfo(org.apache.pulsar.common.schema.SchemaInfo) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) GenericAvroWriter(org.apache.pulsar.client.impl.schema.generic.GenericAvroWriter) TopicName(org.apache.pulsar.common.naming.TopicName) SchemaException(org.apache.pulsar.broker.service.schema.exceptions.SchemaException) ProducerMessage(org.apache.pulsar.websocket.data.ProducerMessage) LocalDateTime(java.time.LocalDateTime) CompletableFuture(java.util.concurrent.CompletableFuture) Message(org.apache.pulsar.client.api.Message) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) GenericJsonWriter(org.apache.pulsar.client.impl.schema.generic.GenericJsonWriter) SchemaType(org.apache.pulsar.common.schema.SchemaType) ProducerMessages(org.apache.pulsar.websocket.data.ProducerMessages) ArrayList(java.util.ArrayList) GenericData(org.apache.avro.generic.GenericData) Commands(org.apache.pulsar.common.protocol.Commands) ByteBuf(io.netty.buffer.ByteBuf) KeyValueEncodingType(org.apache.pulsar.common.schema.KeyValueEncodingType) CompressionCodecProvider(org.apache.pulsar.common.compression.CompressionCodecProvider) Status(javax.ws.rs.core.Response.Status) SchemaRegistry(org.apache.pulsar.broker.service.schema.SchemaRegistry) StringSchema(org.apache.pulsar.client.impl.schema.StringSchema) GenericRecord(org.apache.avro.generic.GenericRecord) GenericAvroRecord(org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord) GenericJsonRecord(org.apache.pulsar.client.impl.schema.generic.GenericJsonRecord) BrokerServiceException(org.apache.pulsar.broker.service.BrokerServiceException) IOException(java.io.IOException) SchemaVersion(org.apache.pulsar.common.protocol.schema.SchemaVersion) Schema(org.apache.pulsar.client.api.Schema) KeyValueSchemaInfo(org.apache.pulsar.client.impl.schema.KeyValueSchemaInfo) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) LookupResult(org.apache.pulsar.broker.lookup.LookupResult) MessageId(org.apache.pulsar.client.api.MessageId) SchemaData(org.apache.pulsar.common.protocol.schema.SchemaData) Collections(java.util.Collections) DecoderFactory(org.apache.avro.io.DecoderFactory) AvroBaseStructSchema(org.apache.pulsar.client.impl.schema.AvroBaseStructSchema) SchemaException(org.apache.pulsar.broker.service.schema.exceptions.SchemaException) CompletableFuture(java.util.concurrent.CompletableFuture) LongSchemaVersion(org.apache.pulsar.common.schema.LongSchemaVersion) SchemaVersion(org.apache.pulsar.common.protocol.schema.SchemaVersion) BrokerServiceException(org.apache.pulsar.broker.service.BrokerServiceException) RestException(org.apache.pulsar.broker.web.RestException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) SchemaException(org.apache.pulsar.broker.service.schema.exceptions.SchemaException) BrokerServiceException(org.apache.pulsar.broker.service.BrokerServiceException) IOException(java.io.IOException)

Example 3 with SchemaException

use of org.apache.pulsar.broker.service.schema.exceptions.SchemaException in project pulsar by apache.

the class TopicsBase method addSchema.

// Add a new schema to schema registry for a topic
private CompletableFuture<SchemaVersion> addSchema(SchemaData schemaData) {
    // Only need to add to first partition the broker owns since the schema id in schema registry are
    // same for all partitions which is the partitionedTopicName
    List<Integer> partitions = pulsar().getBrokerService().getOwningTopics().get(topicName.getPartitionedTopicName()).values();
    CompletableFuture<SchemaVersion> result = new CompletableFuture<>();
    for (int index = 0; index < partitions.size(); index++) {
        CompletableFuture<SchemaVersion> future = new CompletableFuture<>();
        String topicPartitionName = topicName.getPartition(partitions.get(index)).toString();
        pulsar().getBrokerService().getTopic(topicPartitionName, false).thenAccept(topic -> {
            if (!topic.isPresent()) {
                future.completeExceptionally(new BrokerServiceException.TopicNotFoundException("Topic " + topicPartitionName + " not found"));
            } else {
                topic.get().addSchema(schemaData).thenAccept(schemaVersion -> future.complete(schemaVersion)).exceptionally(exception -> {
                    future.completeExceptionally(exception);
                    return null;
                });
            }
        });
        try {
            result.complete(future.get());
            break;
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug("Fail to add schema to topic " + topicName.getPartitionedTopicName() + " for partition " + partitions.get(index) + " for REST produce request.");
            }
        }
    }
    // Not able to add schema to any partition
    if (!result.isDone()) {
        result.completeExceptionally(new SchemaException("Unable to add schema " + schemaData + " to topic " + topicName.getPartitionedTopicName()));
    }
    return result;
}
Also used : PersistentTopicsBase(org.apache.pulsar.broker.admin.impl.PersistentTopicsBase) Arrays(java.util.Arrays) KeyValueSchemaImpl(org.apache.pulsar.client.impl.schema.KeyValueSchemaImpl) Time(java.sql.Time) URISyntaxException(java.net.URISyntaxException) ObjectMapperFactory(org.apache.pulsar.common.util.ObjectMapperFactory) SchemaInfoImpl(org.apache.pulsar.client.impl.schema.SchemaInfoImpl) ByteBuffer(java.nio.ByteBuffer) Decoder(org.apache.avro.io.Decoder) ProducerAck(org.apache.pulsar.websocket.data.ProducerAck) MessageImpl(org.apache.pulsar.client.impl.MessageImpl) Pair(org.apache.commons.lang3.tuple.Pair) LocalTime(java.time.LocalTime) RestException(org.apache.pulsar.broker.web.RestException) URI(java.net.URI) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) DateFormat(java.text.DateFormat) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) CompressionType(org.apache.pulsar.client.api.CompressionType) Timestamp(java.sql.Timestamp) AsyncResponse(javax.ws.rs.container.AsyncResponse) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) DatumReader(org.apache.avro.io.DatumReader) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) MessageIdImpl(org.apache.pulsar.client.impl.MessageIdImpl) ProducerAcks(org.apache.pulsar.websocket.data.ProducerAcks) Base64(java.util.Base64) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) FutureUtil(org.apache.pulsar.common.util.FutureUtil) ConcurrentOpenHashSet(org.apache.pulsar.common.util.collections.ConcurrentOpenHashSet) LookupOptions(org.apache.pulsar.broker.namespace.LookupOptions) MessageMetadata(org.apache.pulsar.common.api.proto.MessageMetadata) Response(javax.ws.rs.core.Response) LocalDate(java.time.LocalDate) LongSchemaVersion(org.apache.pulsar.common.schema.LongSchemaVersion) Optional(java.util.Optional) SchemaInfo(org.apache.pulsar.common.schema.SchemaInfo) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) GenericAvroWriter(org.apache.pulsar.client.impl.schema.generic.GenericAvroWriter) TopicName(org.apache.pulsar.common.naming.TopicName) SchemaException(org.apache.pulsar.broker.service.schema.exceptions.SchemaException) ProducerMessage(org.apache.pulsar.websocket.data.ProducerMessage) LocalDateTime(java.time.LocalDateTime) CompletableFuture(java.util.concurrent.CompletableFuture) Message(org.apache.pulsar.client.api.Message) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) GenericJsonWriter(org.apache.pulsar.client.impl.schema.generic.GenericJsonWriter) SchemaType(org.apache.pulsar.common.schema.SchemaType) ProducerMessages(org.apache.pulsar.websocket.data.ProducerMessages) ArrayList(java.util.ArrayList) GenericData(org.apache.avro.generic.GenericData) Commands(org.apache.pulsar.common.protocol.Commands) ByteBuf(io.netty.buffer.ByteBuf) KeyValueEncodingType(org.apache.pulsar.common.schema.KeyValueEncodingType) CompressionCodecProvider(org.apache.pulsar.common.compression.CompressionCodecProvider) Status(javax.ws.rs.core.Response.Status) SchemaRegistry(org.apache.pulsar.broker.service.schema.SchemaRegistry) StringSchema(org.apache.pulsar.client.impl.schema.StringSchema) GenericRecord(org.apache.avro.generic.GenericRecord) GenericAvroRecord(org.apache.pulsar.client.impl.schema.generic.GenericAvroRecord) GenericJsonRecord(org.apache.pulsar.client.impl.schema.generic.GenericJsonRecord) BrokerServiceException(org.apache.pulsar.broker.service.BrokerServiceException) IOException(java.io.IOException) SchemaVersion(org.apache.pulsar.common.protocol.schema.SchemaVersion) Schema(org.apache.pulsar.client.api.Schema) KeyValueSchemaInfo(org.apache.pulsar.client.impl.schema.KeyValueSchemaInfo) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) LookupResult(org.apache.pulsar.broker.lookup.LookupResult) MessageId(org.apache.pulsar.client.api.MessageId) SchemaData(org.apache.pulsar.common.protocol.schema.SchemaData) Collections(java.util.Collections) DecoderFactory(org.apache.avro.io.DecoderFactory) AvroBaseStructSchema(org.apache.pulsar.client.impl.schema.AvroBaseStructSchema) SchemaException(org.apache.pulsar.broker.service.schema.exceptions.SchemaException) CompletableFuture(java.util.concurrent.CompletableFuture) LongSchemaVersion(org.apache.pulsar.common.schema.LongSchemaVersion) SchemaVersion(org.apache.pulsar.common.protocol.schema.SchemaVersion) BrokerServiceException(org.apache.pulsar.broker.service.BrokerServiceException) URISyntaxException(java.net.URISyntaxException) RestException(org.apache.pulsar.broker.web.RestException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) SchemaException(org.apache.pulsar.broker.service.schema.exceptions.SchemaException) BrokerServiceException(org.apache.pulsar.broker.service.BrokerServiceException) IOException(java.io.IOException)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)3 IOException (java.io.IOException)3 URI (java.net.URI)3 ByteBuffer (java.nio.ByteBuffer)3 Time (java.sql.Time)3 Timestamp (java.sql.Timestamp)3 DateFormat (java.text.DateFormat)3 Instant (java.time.Instant)3 LocalDate (java.time.LocalDate)3 LocalDateTime (java.time.LocalDateTime)3 LocalTime (java.time.LocalTime)3 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 Base64 (java.util.Base64)3 Collections (java.util.Collections)3 List (java.util.List)3 Optional (java.util.Optional)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Collectors (java.util.stream.Collectors)3 AsyncResponse (javax.ws.rs.container.AsyncResponse)3