Search in sources :

Example 1 with SchemaInfoProvider

use of org.apache.pulsar.client.api.schema.SchemaInfoProvider in project pulsar by apache.

the class PulsarClientImpl method preProcessSchemaBeforeSubscribe.

@SuppressWarnings("unchecked")
protected <T> CompletableFuture<Schema<T>> preProcessSchemaBeforeSubscribe(PulsarClientImpl pulsarClientImpl, Schema<T> schema, String topicName) {
    if (schema != null && schema.supportSchemaVersioning()) {
        final SchemaInfoProvider schemaInfoProvider;
        try {
            schemaInfoProvider = pulsarClientImpl.getSchemaProviderLoadingCache().get(topicName);
        } catch (ExecutionException e) {
            log.error("Failed to load schema info provider for topic {}", topicName, e);
            return FutureUtil.failedFuture(e.getCause());
        }
        schema = schema.clone();
        if (schema.requireFetchingSchemaInfo()) {
            @SuppressWarnings("rawtypes") Schema finalSchema = schema;
            return schemaInfoProvider.getLatestSchema().thenCompose(schemaInfo -> {
                if (null == schemaInfo) {
                    if (!(finalSchema instanceof AutoConsumeSchema) && !(finalSchema instanceof KeyValueSchema)) {
                        // no schema info is found
                        return FutureUtil.failedFuture(new PulsarClientException.NotFoundException("No latest schema found for topic " + topicName));
                    }
                }
                try {
                    log.info("Configuring schema for topic {} : {}", topicName, schemaInfo);
                    finalSchema.configureSchemaInfo(topicName, "topic", schemaInfo);
                } catch (RuntimeException re) {
                    return FutureUtil.failedFuture(re);
                }
                finalSchema.setSchemaInfoProvider(schemaInfoProvider);
                return CompletableFuture.completedFuture(finalSchema);
            });
        } else {
            schema.setSchemaInfoProvider(schemaInfoProvider);
        }
    }
    return CompletableFuture.completedFuture(schema);
}
Also used : AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) KeyValueSchema(org.apache.pulsar.client.api.schema.KeyValueSchema) AutoProduceBytesSchema(org.apache.pulsar.client.impl.schema.AutoProduceBytesSchema) Schema(org.apache.pulsar.client.api.Schema) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ExecutionException(java.util.concurrent.ExecutionException) KeyValueSchema(org.apache.pulsar.client.api.schema.KeyValueSchema) MultiVersionSchemaInfoProvider(org.apache.pulsar.client.impl.schema.generic.MultiVersionSchemaInfoProvider) SchemaInfoProvider(org.apache.pulsar.client.api.schema.SchemaInfoProvider)

Example 2 with SchemaInfoProvider

use of org.apache.pulsar.client.api.schema.SchemaInfoProvider in project incubator-pulsar by apache.

the class KeyValueSchemaImpl method setSchemaInfoProviderOnSubschemas.

private void setSchemaInfoProviderOnSubschemas() {
    this.keySchema.setSchemaInfoProvider(new SchemaInfoProvider() {

        @Override
        public CompletableFuture<SchemaInfo> getSchemaByVersion(byte[] schemaVersion) {
            return schemaInfoProvider.getSchemaByVersion(schemaVersion).thenApply(si -> KeyValueSchemaInfo.decodeKeyValueSchemaInfo(si).getKey());
        }

        @Override
        public CompletableFuture<SchemaInfo> getLatestSchema() {
            return CompletableFuture.completedFuture(((AbstractStructSchema<K>) keySchema).schemaInfo);
        }

        @Override
        public String getTopicName() {
            return "key-schema";
        }
    });
    this.valueSchema.setSchemaInfoProvider(new SchemaInfoProvider() {

        @Override
        public CompletableFuture<SchemaInfo> getSchemaByVersion(byte[] schemaVersion) {
            return schemaInfoProvider.getSchemaByVersion(schemaVersion).thenApply(si -> KeyValueSchemaInfo.decodeKeyValueSchemaInfo(si).getValue());
        }

        @Override
        public CompletableFuture<SchemaInfo> getLatestSchema() {
            return CompletableFuture.completedFuture(((AbstractStructSchema<V>) valueSchema).schemaInfo);
        }

        @Override
        public String getTopicName() {
            return "value-schema";
        }
    });
}
Also used : SchemaInfoProvider(org.apache.pulsar.client.api.schema.SchemaInfoProvider) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) CompletableFuture(java.util.concurrent.CompletableFuture) SchemaVersion(org.apache.pulsar.common.protocol.schema.SchemaVersion) SchemaType(org.apache.pulsar.common.schema.SchemaType) Schema(org.apache.pulsar.client.api.Schema) ExecutionException(java.util.concurrent.ExecutionException) KeyValue(org.apache.pulsar.common.schema.KeyValue) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ByteBufUtil(io.netty.buffer.ByteBufUtil) Slf4j(lombok.extern.slf4j.Slf4j) ByteBuf(io.netty.buffer.ByteBuf) BytesSchemaVersion(org.apache.pulsar.common.protocol.schema.BytesSchemaVersion) Map(java.util.Map) KeyValueEncodingType(org.apache.pulsar.common.schema.KeyValueEncodingType) KeyValueSchema(org.apache.pulsar.client.api.schema.KeyValueSchema) SchemaInfo(org.apache.pulsar.common.schema.SchemaInfo) CompletableFuture(java.util.concurrent.CompletableFuture) SchemaInfoProvider(org.apache.pulsar.client.api.schema.SchemaInfoProvider)

Example 3 with SchemaInfoProvider

use of org.apache.pulsar.client.api.schema.SchemaInfoProvider in project incubator-pulsar by apache.

the class PulsarClientImpl method preProcessSchemaBeforeSubscribe.

@SuppressWarnings("unchecked")
protected <T> CompletableFuture<Schema<T>> preProcessSchemaBeforeSubscribe(PulsarClientImpl pulsarClientImpl, Schema<T> schema, String topicName) {
    if (schema != null && schema.supportSchemaVersioning()) {
        final SchemaInfoProvider schemaInfoProvider;
        try {
            schemaInfoProvider = pulsarClientImpl.getSchemaProviderLoadingCache().get(topicName);
        } catch (ExecutionException e) {
            log.error("Failed to load schema info provider for topic {}", topicName, e);
            return FutureUtil.failedFuture(e.getCause());
        }
        schema = schema.clone();
        if (schema.requireFetchingSchemaInfo()) {
            @SuppressWarnings("rawtypes") Schema finalSchema = schema;
            return schemaInfoProvider.getLatestSchema().thenCompose(schemaInfo -> {
                if (null == schemaInfo) {
                    if (!(finalSchema instanceof AutoConsumeSchema) && !(finalSchema instanceof KeyValueSchema)) {
                        // no schema info is found
                        return FutureUtil.failedFuture(new PulsarClientException.NotFoundException("No latest schema found for topic " + topicName));
                    }
                }
                try {
                    log.info("Configuring schema for topic {} : {}", topicName, schemaInfo);
                    finalSchema.configureSchemaInfo(topicName, "topic", schemaInfo);
                } catch (RuntimeException re) {
                    return FutureUtil.failedFuture(re);
                }
                finalSchema.setSchemaInfoProvider(schemaInfoProvider);
                return CompletableFuture.completedFuture(finalSchema);
            });
        } else {
            schema.setSchemaInfoProvider(schemaInfoProvider);
        }
    }
    return CompletableFuture.completedFuture(schema);
}
Also used : AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) KeyValueSchema(org.apache.pulsar.client.api.schema.KeyValueSchema) AutoProduceBytesSchema(org.apache.pulsar.client.impl.schema.AutoProduceBytesSchema) Schema(org.apache.pulsar.client.api.Schema) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ExecutionException(java.util.concurrent.ExecutionException) KeyValueSchema(org.apache.pulsar.client.api.schema.KeyValueSchema) MultiVersionSchemaInfoProvider(org.apache.pulsar.client.impl.schema.generic.MultiVersionSchemaInfoProvider) SchemaInfoProvider(org.apache.pulsar.client.api.schema.SchemaInfoProvider)

Example 4 with SchemaInfoProvider

use of org.apache.pulsar.client.api.schema.SchemaInfoProvider in project pulsar by apache.

the class KeyValueSchemaImpl method setSchemaInfoProviderOnSubschemas.

private void setSchemaInfoProviderOnSubschemas() {
    this.keySchema.setSchemaInfoProvider(new SchemaInfoProvider() {

        @Override
        public CompletableFuture<SchemaInfo> getSchemaByVersion(byte[] schemaVersion) {
            return schemaInfoProvider.getSchemaByVersion(schemaVersion).thenApply(si -> KeyValueSchemaInfo.decodeKeyValueSchemaInfo(si).getKey());
        }

        @Override
        public CompletableFuture<SchemaInfo> getLatestSchema() {
            return CompletableFuture.completedFuture(((AbstractStructSchema<K>) keySchema).schemaInfo);
        }

        @Override
        public String getTopicName() {
            return "key-schema";
        }
    });
    this.valueSchema.setSchemaInfoProvider(new SchemaInfoProvider() {

        @Override
        public CompletableFuture<SchemaInfo> getSchemaByVersion(byte[] schemaVersion) {
            return schemaInfoProvider.getSchemaByVersion(schemaVersion).thenApply(si -> KeyValueSchemaInfo.decodeKeyValueSchemaInfo(si).getValue());
        }

        @Override
        public CompletableFuture<SchemaInfo> getLatestSchema() {
            return CompletableFuture.completedFuture(((AbstractStructSchema<V>) valueSchema).schemaInfo);
        }

        @Override
        public String getTopicName() {
            return "value-schema";
        }
    });
}
Also used : SchemaInfoProvider(org.apache.pulsar.client.api.schema.SchemaInfoProvider) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) CompletableFuture(java.util.concurrent.CompletableFuture) SchemaVersion(org.apache.pulsar.common.protocol.schema.SchemaVersion) SchemaType(org.apache.pulsar.common.schema.SchemaType) Schema(org.apache.pulsar.client.api.Schema) ExecutionException(java.util.concurrent.ExecutionException) KeyValue(org.apache.pulsar.common.schema.KeyValue) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ByteBufUtil(io.netty.buffer.ByteBufUtil) Slf4j(lombok.extern.slf4j.Slf4j) ByteBuf(io.netty.buffer.ByteBuf) BytesSchemaVersion(org.apache.pulsar.common.protocol.schema.BytesSchemaVersion) KeyValueEncodingType(org.apache.pulsar.common.schema.KeyValueEncodingType) KeyValueSchema(org.apache.pulsar.client.api.schema.KeyValueSchema) SchemaInfo(org.apache.pulsar.common.schema.SchemaInfo) CompletableFuture(java.util.concurrent.CompletableFuture) SchemaInfoProvider(org.apache.pulsar.client.api.schema.SchemaInfoProvider)

Example 5 with SchemaInfoProvider

use of org.apache.pulsar.client.api.schema.SchemaInfoProvider in project pulsar by yahoo.

the class PulsarClientImpl method preProcessSchemaBeforeSubscribe.

@SuppressWarnings("unchecked")
protected <T> CompletableFuture<Schema<T>> preProcessSchemaBeforeSubscribe(PulsarClientImpl pulsarClientImpl, Schema<T> schema, String topicName) {
    if (schema != null && schema.supportSchemaVersioning()) {
        final SchemaInfoProvider schemaInfoProvider;
        try {
            schemaInfoProvider = pulsarClientImpl.getSchemaProviderLoadingCache().get(topicName);
        } catch (ExecutionException e) {
            log.error("Failed to load schema info provider for topic {}", topicName, e);
            return FutureUtil.failedFuture(e.getCause());
        }
        schema = schema.clone();
        if (schema.requireFetchingSchemaInfo()) {
            @SuppressWarnings("rawtypes") Schema finalSchema = schema;
            return schemaInfoProvider.getLatestSchema().thenCompose(schemaInfo -> {
                if (null == schemaInfo) {
                    if (!(finalSchema instanceof AutoConsumeSchema) && !(finalSchema instanceof KeyValueSchema)) {
                        // no schema info is found
                        return FutureUtil.failedFuture(new PulsarClientException.NotFoundException("No latest schema found for topic " + topicName));
                    }
                }
                try {
                    log.info("Configuring schema for topic {} : {}", topicName, schemaInfo);
                    finalSchema.configureSchemaInfo(topicName, "topic", schemaInfo);
                } catch (RuntimeException re) {
                    return FutureUtil.failedFuture(re);
                }
                finalSchema.setSchemaInfoProvider(schemaInfoProvider);
                return CompletableFuture.completedFuture(finalSchema);
            });
        } else {
            schema.setSchemaInfoProvider(schemaInfoProvider);
        }
    }
    return CompletableFuture.completedFuture(schema);
}
Also used : AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) KeyValueSchema(org.apache.pulsar.client.api.schema.KeyValueSchema) AutoProduceBytesSchema(org.apache.pulsar.client.impl.schema.AutoProduceBytesSchema) Schema(org.apache.pulsar.client.api.Schema) AutoConsumeSchema(org.apache.pulsar.client.impl.schema.AutoConsumeSchema) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ExecutionException(java.util.concurrent.ExecutionException) KeyValueSchema(org.apache.pulsar.client.api.schema.KeyValueSchema) MultiVersionSchemaInfoProvider(org.apache.pulsar.client.impl.schema.generic.MultiVersionSchemaInfoProvider) SchemaInfoProvider(org.apache.pulsar.client.api.schema.SchemaInfoProvider)

Aggregations

ExecutionException (java.util.concurrent.ExecutionException)6 Schema (org.apache.pulsar.client.api.Schema)6 KeyValueSchema (org.apache.pulsar.client.api.schema.KeyValueSchema)6 SchemaInfoProvider (org.apache.pulsar.client.api.schema.SchemaInfoProvider)6 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)3 ByteBuf (io.netty.buffer.ByteBuf)3 ByteBufUtil (io.netty.buffer.ByteBufUtil)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Slf4j (lombok.extern.slf4j.Slf4j)3 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)3 SchemaSerializationException (org.apache.pulsar.client.api.SchemaSerializationException)3 AutoConsumeSchema (org.apache.pulsar.client.impl.schema.AutoConsumeSchema)3 AutoProduceBytesSchema (org.apache.pulsar.client.impl.schema.AutoProduceBytesSchema)3 MultiVersionSchemaInfoProvider (org.apache.pulsar.client.impl.schema.generic.MultiVersionSchemaInfoProvider)3 BytesSchemaVersion (org.apache.pulsar.common.protocol.schema.BytesSchemaVersion)3 SchemaVersion (org.apache.pulsar.common.protocol.schema.SchemaVersion)3 KeyValue (org.apache.pulsar.common.schema.KeyValue)3 KeyValueEncodingType (org.apache.pulsar.common.schema.KeyValueEncodingType)3 SchemaInfo (org.apache.pulsar.common.schema.SchemaInfo)3 SchemaType (org.apache.pulsar.common.schema.SchemaType)3