Search in sources :

Example 1 with ProducerConfig

use of org.apache.pulsar.common.functions.ProducerConfig in project pulsar by apache.

the class FunctionConfigUtilsTest method testConvertBackFidelity.

@Test
public void testConvertBackFidelity() {
    FunctionConfig functionConfig = new FunctionConfig();
    functionConfig.setTenant("test-tenant");
    functionConfig.setNamespace("test-namespace");
    functionConfig.setName("test-function");
    functionConfig.setParallelism(1);
    functionConfig.setClassName(IdentityFunction.class.getName());
    Map<String, ConsumerConfig> inputSpecs = new HashMap<>();
    inputSpecs.put("test-input", ConsumerConfig.builder().isRegexPattern(true).serdeClassName("test-serde").poolMessages(true).build());
    functionConfig.setInputSpecs(inputSpecs);
    functionConfig.setOutput("test-output");
    functionConfig.setOutputSerdeClassName("test-serde");
    functionConfig.setRuntime(FunctionConfig.Runtime.JAVA);
    functionConfig.setProcessingGuarantees(FunctionConfig.ProcessingGuarantees.ATLEAST_ONCE);
    functionConfig.setRetainOrdering(false);
    functionConfig.setRetainKeyOrdering(false);
    functionConfig.setForwardSourceMessageProperty(true);
    functionConfig.setUserConfig(new HashMap<>());
    functionConfig.setAutoAck(true);
    functionConfig.setTimeoutMs(2000l);
    functionConfig.setRuntimeFlags("-DKerberos");
    ProducerConfig producerConfig = new ProducerConfig();
    producerConfig.setMaxPendingMessages(100);
    producerConfig.setMaxPendingMessagesAcrossPartitions(1000);
    producerConfig.setUseThreadLocalProducers(true);
    producerConfig.setBatchBuilder("DEFAULT");
    functionConfig.setProducerConfig(producerConfig);
    Function.FunctionDetails functionDetails = FunctionConfigUtils.convert(functionConfig, null);
    FunctionConfig convertedConfig = FunctionConfigUtils.convertFromDetails(functionDetails);
    // add default resources
    functionConfig.setResources(Resources.getDefaultResources());
    // set default cleanupSubscription config
    functionConfig.setCleanupSubscription(true);
    assertEquals(new Gson().toJson(functionConfig), new Gson().toJson(convertedConfig));
}
Also used : FunctionConfig(org.apache.pulsar.common.functions.FunctionConfig) IdentityFunction(org.apache.pulsar.functions.api.utils.IdentityFunction) Function(org.apache.pulsar.functions.proto.Function) HashMap(java.util.HashMap) IdentityFunction(org.apache.pulsar.functions.api.utils.IdentityFunction) FunctionDetails(org.apache.pulsar.functions.proto.Function.FunctionDetails) ProducerConfig(org.apache.pulsar.common.functions.ProducerConfig) Gson(com.google.gson.Gson) ConsumerConfig(org.apache.pulsar.common.functions.ConsumerConfig) Test(org.testng.annotations.Test)

Example 2 with ProducerConfig

use of org.apache.pulsar.common.functions.ProducerConfig in project pulsar by apache.

the class FunctionConfigUtils method convert.

public static FunctionDetails convert(FunctionConfig functionConfig, ClassLoader classLoader) throws IllegalArgumentException {
    boolean isBuiltin = !org.apache.commons.lang3.StringUtils.isEmpty(functionConfig.getJar()) && functionConfig.getJar().startsWith(org.apache.pulsar.common.functions.Utils.BUILTIN);
    Class<?>[] typeArgs = null;
    if (functionConfig.getRuntime() == FunctionConfig.Runtime.JAVA) {
        if (classLoader != null) {
            try {
                typeArgs = FunctionCommon.getFunctionTypes(functionConfig, classLoader);
            } catch (ClassNotFoundException | NoClassDefFoundError e) {
                throw new IllegalArgumentException(String.format("Function class %s must be in class path", functionConfig.getClassName()), e);
            }
        }
    }
    FunctionDetails.Builder functionDetailsBuilder = FunctionDetails.newBuilder();
    // Setup source
    Function.SourceSpec.Builder sourceSpecBuilder = Function.SourceSpec.newBuilder();
    if (functionConfig.getInputs() != null) {
        functionConfig.getInputs().forEach((topicName -> {
            sourceSpecBuilder.putInputSpecs(topicName, Function.ConsumerSpec.newBuilder().setIsRegexPattern(false).build());
        }));
    }
    if (functionConfig.getTopicsPattern() != null && !functionConfig.getTopicsPattern().isEmpty()) {
        sourceSpecBuilder.putInputSpecs(functionConfig.getTopicsPattern(), Function.ConsumerSpec.newBuilder().setIsRegexPattern(true).build());
    }
    if (functionConfig.getCustomSerdeInputs() != null) {
        functionConfig.getCustomSerdeInputs().forEach((topicName, serdeClassName) -> {
            sourceSpecBuilder.putInputSpecs(topicName, Function.ConsumerSpec.newBuilder().setSerdeClassName(serdeClassName).setIsRegexPattern(false).build());
        });
    }
    if (functionConfig.getCustomSchemaInputs() != null) {
        functionConfig.getCustomSchemaInputs().forEach((topicName, conf) -> {
            try {
                ConsumerConfig consumerConfig = OBJECT_MAPPER.readValue(conf, ConsumerConfig.class);
                sourceSpecBuilder.putInputSpecs(topicName, Function.ConsumerSpec.newBuilder().setSchemaType(consumerConfig.getSchemaType()).putAllSchemaProperties(consumerConfig.getSchemaProperties()).putAllConsumerProperties(consumerConfig.getConsumerProperties()).setIsRegexPattern(false).build());
            } catch (JsonProcessingException e) {
                throw new IllegalArgumentException(String.format("Incorrect custom schema inputs,Topic %s ", topicName));
            }
        });
    }
    if (functionConfig.getInputSpecs() != null) {
        functionConfig.getInputSpecs().forEach((topicName, consumerConf) -> {
            Function.ConsumerSpec.Builder bldr = Function.ConsumerSpec.newBuilder().setIsRegexPattern(consumerConf.isRegexPattern());
            if (isNotBlank(consumerConf.getSchemaType())) {
                bldr.setSchemaType(consumerConf.getSchemaType());
            } else if (isNotBlank(consumerConf.getSerdeClassName())) {
                bldr.setSerdeClassName(consumerConf.getSerdeClassName());
            }
            if (consumerConf.getReceiverQueueSize() != null) {
                bldr.setReceiverQueueSize(Function.ConsumerSpec.ReceiverQueueSize.newBuilder().setValue(consumerConf.getReceiverQueueSize()).build());
            }
            if (consumerConf.getSchemaProperties() != null) {
                bldr.putAllSchemaProperties(consumerConf.getSchemaProperties());
            }
            if (consumerConf.getCryptoConfig() != null) {
                bldr.setCryptoSpec(CryptoUtils.convert(consumerConf.getCryptoConfig()));
            }
            bldr.putAllConsumerProperties(consumerConf.getConsumerProperties());
            bldr.setPoolMessages(consumerConf.isPoolMessages());
            sourceSpecBuilder.putInputSpecs(topicName, bldr.build());
        });
    }
    // Set subscription type
    Function.SubscriptionType subType;
    if ((functionConfig.getRetainOrdering() != null && functionConfig.getRetainOrdering()) || FunctionConfig.ProcessingGuarantees.EFFECTIVELY_ONCE.equals(functionConfig.getProcessingGuarantees())) {
        subType = Function.SubscriptionType.FAILOVER;
    } else if (functionConfig.getRetainKeyOrdering() != null && functionConfig.getRetainKeyOrdering()) {
        subType = Function.SubscriptionType.KEY_SHARED;
    } else {
        subType = Function.SubscriptionType.SHARED;
    }
    sourceSpecBuilder.setSubscriptionType(subType);
    // Set subscription name
    if (isNotBlank(functionConfig.getSubName())) {
        sourceSpecBuilder.setSubscriptionName(functionConfig.getSubName());
    }
    // Set subscription position
    if (functionConfig.getSubscriptionPosition() != null) {
        Function.SubscriptionPosition subPosition = null;
        if (SubscriptionInitialPosition.Earliest == functionConfig.getSubscriptionPosition()) {
            subPosition = Function.SubscriptionPosition.EARLIEST;
        } else {
            subPosition = Function.SubscriptionPosition.LATEST;
        }
        sourceSpecBuilder.setSubscriptionPosition(subPosition);
    }
    if (typeArgs != null) {
        sourceSpecBuilder.setTypeClassName(typeArgs[0].getName());
    }
    if (functionConfig.getTimeoutMs() != null) {
        sourceSpecBuilder.setTimeoutMs(functionConfig.getTimeoutMs());
        // We use negative acks for fast tracking failures
        sourceSpecBuilder.setNegativeAckRedeliveryDelayMs(functionConfig.getTimeoutMs());
    }
    if (functionConfig.getCleanupSubscription() != null) {
        sourceSpecBuilder.setCleanupSubscription(functionConfig.getCleanupSubscription());
    } else {
        sourceSpecBuilder.setCleanupSubscription(true);
    }
    functionDetailsBuilder.setSource(sourceSpecBuilder);
    // Setup sink
    Function.SinkSpec.Builder sinkSpecBuilder = Function.SinkSpec.newBuilder();
    if (functionConfig.getOutput() != null) {
        sinkSpecBuilder.setTopic(functionConfig.getOutput());
    }
    if (!StringUtils.isBlank(functionConfig.getOutputSerdeClassName())) {
        sinkSpecBuilder.setSerDeClassName(functionConfig.getOutputSerdeClassName());
    }
    if (!StringUtils.isBlank(functionConfig.getOutputSchemaType())) {
        sinkSpecBuilder.setSchemaType(functionConfig.getOutputSchemaType());
    }
    if (functionConfig.getForwardSourceMessageProperty() == Boolean.TRUE) {
        sinkSpecBuilder.setForwardSourceMessageProperty(functionConfig.getForwardSourceMessageProperty());
    }
    if (functionConfig.getCustomSchemaOutputs() != null && functionConfig.getOutput() != null) {
        String conf = functionConfig.getCustomSchemaOutputs().get(functionConfig.getOutput());
        try {
            if (StringUtils.isNotEmpty(conf)) {
                ConsumerConfig consumerConfig = OBJECT_MAPPER.readValue(conf, ConsumerConfig.class);
                sinkSpecBuilder.putAllSchemaProperties(consumerConfig.getSchemaProperties());
                sinkSpecBuilder.putAllConsumerProperties(consumerConfig.getConsumerProperties());
            }
        } catch (JsonProcessingException e) {
            throw new IllegalArgumentException(String.format("Incorrect custom schema outputs,Topic %s ", functionConfig.getOutput()));
        }
    }
    if (typeArgs != null) {
        sinkSpecBuilder.setTypeClassName(typeArgs[1].getName());
    }
    if (functionConfig.getProducerConfig() != null) {
        ProducerConfig producerConf = functionConfig.getProducerConfig();
        Function.ProducerSpec.Builder pbldr = Function.ProducerSpec.newBuilder();
        if (producerConf.getMaxPendingMessages() != null) {
            pbldr.setMaxPendingMessages(producerConf.getMaxPendingMessages());
        }
        if (producerConf.getMaxPendingMessagesAcrossPartitions() != null) {
            pbldr.setMaxPendingMessagesAcrossPartitions(producerConf.getMaxPendingMessagesAcrossPartitions());
        }
        if (producerConf.getUseThreadLocalProducers() != null) {
            pbldr.setUseThreadLocalProducers(producerConf.getUseThreadLocalProducers());
        }
        if (producerConf.getCryptoConfig() != null) {
            pbldr.setCryptoSpec(CryptoUtils.convert(producerConf.getCryptoConfig()));
        }
        if (producerConf.getBatchBuilder() != null) {
            pbldr.setBatchBuilder(producerConf.getBatchBuilder());
        }
        sinkSpecBuilder.setProducerSpec(pbldr.build());
    }
    functionDetailsBuilder.setSink(sinkSpecBuilder);
    if (functionConfig.getTenant() != null) {
        functionDetailsBuilder.setTenant(functionConfig.getTenant());
    }
    if (functionConfig.getNamespace() != null) {
        functionDetailsBuilder.setNamespace(functionConfig.getNamespace());
    }
    if (functionConfig.getName() != null) {
        functionDetailsBuilder.setName(functionConfig.getName());
    }
    if (functionConfig.getLogTopic() != null) {
        functionDetailsBuilder.setLogTopic(functionConfig.getLogTopic());
    }
    if (functionConfig.getRuntime() != null) {
        functionDetailsBuilder.setRuntime(FunctionCommon.convertRuntime(functionConfig.getRuntime()));
    }
    if (functionConfig.getProcessingGuarantees() != null) {
        functionDetailsBuilder.setProcessingGuarantees(FunctionCommon.convertProcessingGuarantee(functionConfig.getProcessingGuarantees()));
    }
    if (functionConfig.getRetainKeyOrdering() != null) {
        functionDetailsBuilder.setRetainKeyOrdering(functionConfig.getRetainKeyOrdering());
    }
    if (functionConfig.getRetainOrdering() != null) {
        functionDetailsBuilder.setRetainOrdering(functionConfig.getRetainOrdering());
    }
    if (functionConfig.getMaxMessageRetries() != null && functionConfig.getMaxMessageRetries() >= 0) {
        Function.RetryDetails.Builder retryBuilder = Function.RetryDetails.newBuilder();
        retryBuilder.setMaxMessageRetries(functionConfig.getMaxMessageRetries());
        if (isNotEmpty(functionConfig.getDeadLetterTopic())) {
            retryBuilder.setDeadLetterTopic(functionConfig.getDeadLetterTopic());
        }
        functionDetailsBuilder.setRetryDetails(retryBuilder);
    }
    Map<String, Object> configs = new HashMap<>();
    if (functionConfig.getUserConfig() != null) {
        configs.putAll(functionConfig.getUserConfig());
    }
    // windowing related
    WindowConfig windowConfig = functionConfig.getWindowConfig();
    if (windowConfig != null) {
        windowConfig.setActualWindowFunctionClassName(functionConfig.getClassName());
        configs.put(WindowConfig.WINDOW_CONFIG_KEY, windowConfig);
        // set class name to window function executor
        functionDetailsBuilder.setClassName("org.apache.pulsar.functions.windowing.WindowFunctionExecutor");
    } else {
        if (functionConfig.getClassName() != null) {
            functionDetailsBuilder.setClassName(functionConfig.getClassName());
        }
    }
    if (!configs.isEmpty()) {
        functionDetailsBuilder.setUserConfig(new Gson().toJson(configs));
    }
    if (functionConfig.getSecrets() != null && !functionConfig.getSecrets().isEmpty()) {
        functionDetailsBuilder.setSecretsMap(new Gson().toJson(functionConfig.getSecrets()));
    }
    if (functionConfig.getAutoAck() != null) {
        functionDetailsBuilder.setAutoAck(functionConfig.getAutoAck());
    } else {
        functionDetailsBuilder.setAutoAck(true);
    }
    if (functionConfig.getParallelism() != null) {
        functionDetailsBuilder.setParallelism(functionConfig.getParallelism());
    } else {
        functionDetailsBuilder.setParallelism(1);
    }
    // use default resources if resources not set
    Resources resources = Resources.mergeWithDefault(functionConfig.getResources());
    Function.Resources.Builder bldr = Function.Resources.newBuilder();
    bldr.setCpu(resources.getCpu());
    bldr.setRam(resources.getRam());
    bldr.setDisk(resources.getDisk());
    functionDetailsBuilder.setResources(bldr);
    if (!StringUtils.isEmpty(functionConfig.getRuntimeFlags())) {
        functionDetailsBuilder.setRuntimeFlags(functionConfig.getRuntimeFlags());
    }
    functionDetailsBuilder.setComponentType(FunctionDetails.ComponentType.FUNCTION);
    if (!StringUtils.isEmpty(functionConfig.getCustomRuntimeOptions())) {
        functionDetailsBuilder.setCustomRuntimeOptions(functionConfig.getCustomRuntimeOptions());
    }
    if (isBuiltin) {
        String builtin = functionConfig.getJar().replaceFirst("^builtin://", "");
        functionDetailsBuilder.setBuiltin(builtin);
    }
    return functionDetailsBuilder.build();
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) TypeToken(com.google.gson.reflect.TypeToken) TopicName(org.apache.pulsar.common.naming.TopicName) ObjectMapperFactory(org.apache.pulsar.common.util.ObjectMapperFactory) StringUtils.isNotEmpty(org.apache.commons.lang.StringUtils.isNotEmpty) HashMap(java.util.HashMap) Resources(org.apache.pulsar.common.functions.Resources) ClassLoaderUtils.loadJar(org.apache.pulsar.common.util.ClassLoaderUtils.loadJar) SubscriptionInitialPosition(org.apache.pulsar.client.api.SubscriptionInitialPosition) StringUtils.isBlank(org.apache.commons.lang.StringUtils.isBlank) FunctionDetails(org.apache.pulsar.functions.proto.Function.FunctionDetails) Gson(com.google.gson.Gson) Map(java.util.Map) FunctionConfig(org.apache.pulsar.common.functions.FunctionConfig) LinkedList(java.util.LinkedList) StringUtils.isEmpty(org.apache.commons.lang3.StringUtils.isEmpty) FunctionCommon.convertFromFunctionDetailsSubscriptionPosition(org.apache.pulsar.functions.utils.FunctionCommon.convertFromFunctionDetailsSubscriptionPosition) Function(org.apache.pulsar.functions.proto.Function) WindowConfig(org.apache.pulsar.common.functions.WindowConfig) BUILTIN(org.apache.pulsar.common.functions.Utils.BUILTIN) MalformedURLException(java.net.MalformedURLException) Collection(java.util.Collection) ConsumerConfig(org.apache.pulsar.common.functions.ConsumerConfig) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) StringUtils.isNotBlank(org.apache.commons.lang.StringUtils.isNotBlank) File(java.io.File) ProducerConfig(org.apache.pulsar.common.functions.ProducerConfig) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Type(java.lang.reflect.Type) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) Function(org.apache.pulsar.functions.proto.Function) ProducerConfig(org.apache.pulsar.common.functions.ProducerConfig) ConsumerConfig(org.apache.pulsar.common.functions.ConsumerConfig) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) WindowConfig(org.apache.pulsar.common.functions.WindowConfig) FunctionDetails(org.apache.pulsar.functions.proto.Function.FunctionDetails) Resources(org.apache.pulsar.common.functions.Resources)

Example 3 with ProducerConfig

use of org.apache.pulsar.common.functions.ProducerConfig in project pulsar by apache.

the class SourceConfigUtils method convertFromDetails.

public static SourceConfig convertFromDetails(FunctionDetails functionDetails) {
    SourceConfig sourceConfig = new SourceConfig();
    sourceConfig.setTenant(functionDetails.getTenant());
    sourceConfig.setNamespace(functionDetails.getNamespace());
    sourceConfig.setName(functionDetails.getName());
    sourceConfig.setParallelism(functionDetails.getParallelism());
    sourceConfig.setProcessingGuarantees(FunctionCommon.convertProcessingGuarantee(functionDetails.getProcessingGuarantees()));
    Function.SourceSpec sourceSpec = functionDetails.getSource();
    if (!StringUtils.isEmpty(sourceSpec.getClassName())) {
        sourceConfig.setClassName(sourceSpec.getClassName());
    }
    if (!StringUtils.isEmpty(sourceSpec.getBuiltin())) {
        sourceConfig.setArchive("builtin://" + sourceSpec.getBuiltin());
    }
    Map<String, Object> configMap = extractSourceConfig(sourceSpec, FunctionCommon.getFullyQualifiedName(functionDetails));
    if (configMap != null) {
        BatchSourceConfig batchSourceConfig = extractBatchSourceConfig(configMap);
        if (batchSourceConfig != null) {
            sourceConfig.setBatchSourceConfig(batchSourceConfig);
            if (configMap.containsKey(BatchSourceConfig.BATCHSOURCE_CLASSNAME_KEY)) {
                if (!StringUtils.isEmpty((String) configMap.get(BatchSourceConfig.BATCHSOURCE_CLASSNAME_KEY))) {
                    sourceConfig.setClassName((String) configMap.get(BatchSourceConfig.BATCHSOURCE_CLASSNAME_KEY));
                } else {
                    sourceConfig.setClassName(null);
                }
            }
        }
        configMap.remove(BatchSourceConfig.BATCHSOURCE_CONFIG_KEY);
        configMap.remove(BatchSourceConfig.BATCHSOURCE_CLASSNAME_KEY);
        sourceConfig.setConfigs(configMap);
    }
    if (!isEmpty(functionDetails.getSecretsMap())) {
        Type type = new TypeToken<Map<String, Object>>() {
        }.getType();
        Map<String, Object> secretsMap = new Gson().fromJson(functionDetails.getSecretsMap(), type);
        sourceConfig.setSecrets(secretsMap);
    }
    Function.SinkSpec sinkSpec = functionDetails.getSink();
    sourceConfig.setTopicName(sinkSpec.getTopic());
    if (!StringUtils.isEmpty(sinkSpec.getSchemaType())) {
        sourceConfig.setSchemaType(sinkSpec.getSchemaType());
    }
    if (!StringUtils.isEmpty(sinkSpec.getSerDeClassName())) {
        sourceConfig.setSerdeClassName(sinkSpec.getSerDeClassName());
    }
    if (sinkSpec.getProducerSpec() != null) {
        Function.ProducerSpec spec = sinkSpec.getProducerSpec();
        ProducerConfig producerConfig = new ProducerConfig();
        if (spec.getMaxPendingMessages() != 0) {
            producerConfig.setMaxPendingMessages(spec.getMaxPendingMessages());
        }
        if (spec.getMaxPendingMessagesAcrossPartitions() != 0) {
            producerConfig.setMaxPendingMessagesAcrossPartitions(spec.getMaxPendingMessagesAcrossPartitions());
        }
        if (spec.hasCryptoSpec()) {
            producerConfig.setCryptoConfig(CryptoUtils.convertFromSpec(spec.getCryptoSpec()));
        }
        if (spec.getBatchBuilder() != null) {
            producerConfig.setBatchBuilder(spec.getBatchBuilder());
        }
        producerConfig.setUseThreadLocalProducers(spec.getUseThreadLocalProducers());
        sourceConfig.setProducerConfig(producerConfig);
    }
    if (functionDetails.hasResources()) {
        Resources resources = new Resources();
        resources.setCpu(functionDetails.getResources().getCpu());
        resources.setRam(functionDetails.getResources().getRam());
        resources.setDisk(functionDetails.getResources().getDisk());
        sourceConfig.setResources(resources);
    }
    if (!isEmpty(functionDetails.getRuntimeFlags())) {
        sourceConfig.setRuntimeFlags(functionDetails.getRuntimeFlags());
    }
    if (!isEmpty(functionDetails.getCustomRuntimeOptions())) {
        sourceConfig.setCustomRuntimeOptions(functionDetails.getCustomRuntimeOptions());
    }
    return sourceConfig;
}
Also used : BatchSourceConfig(org.apache.pulsar.common.io.BatchSourceConfig) BatchSourceConfig(org.apache.pulsar.common.io.BatchSourceConfig) SourceConfig(org.apache.pulsar.common.io.SourceConfig) Gson(com.google.gson.Gson) IdentityFunction(org.apache.pulsar.functions.api.utils.IdentityFunction) Function(org.apache.pulsar.functions.proto.Function) FunctionCommon.getSourceType(org.apache.pulsar.functions.utils.FunctionCommon.getSourceType) Type(java.lang.reflect.Type) ProducerConfig(org.apache.pulsar.common.functions.ProducerConfig) Resources(org.apache.pulsar.common.functions.Resources) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with ProducerConfig

use of org.apache.pulsar.common.functions.ProducerConfig in project pulsar by yahoo.

the class FunctionConfigUtilsTest method testConvertBackFidelity.

@Test
public void testConvertBackFidelity() {
    FunctionConfig functionConfig = new FunctionConfig();
    functionConfig.setTenant("test-tenant");
    functionConfig.setNamespace("test-namespace");
    functionConfig.setName("test-function");
    functionConfig.setParallelism(1);
    functionConfig.setClassName(IdentityFunction.class.getName());
    Map<String, ConsumerConfig> inputSpecs = new HashMap<>();
    inputSpecs.put("test-input", ConsumerConfig.builder().isRegexPattern(true).serdeClassName("test-serde").poolMessages(true).build());
    functionConfig.setInputSpecs(inputSpecs);
    functionConfig.setOutput("test-output");
    functionConfig.setOutputSerdeClassName("test-serde");
    functionConfig.setRuntime(FunctionConfig.Runtime.JAVA);
    functionConfig.setProcessingGuarantees(FunctionConfig.ProcessingGuarantees.ATLEAST_ONCE);
    functionConfig.setRetainOrdering(false);
    functionConfig.setRetainKeyOrdering(false);
    functionConfig.setForwardSourceMessageProperty(true);
    functionConfig.setUserConfig(new HashMap<>());
    functionConfig.setAutoAck(true);
    functionConfig.setTimeoutMs(2000l);
    functionConfig.setRuntimeFlags("-DKerberos");
    ProducerConfig producerConfig = new ProducerConfig();
    producerConfig.setMaxPendingMessages(100);
    producerConfig.setMaxPendingMessagesAcrossPartitions(1000);
    producerConfig.setUseThreadLocalProducers(true);
    producerConfig.setBatchBuilder("DEFAULT");
    functionConfig.setProducerConfig(producerConfig);
    Function.FunctionDetails functionDetails = FunctionConfigUtils.convert(functionConfig, null);
    FunctionConfig convertedConfig = FunctionConfigUtils.convertFromDetails(functionDetails);
    // add default resources
    functionConfig.setResources(Resources.getDefaultResources());
    // set default cleanupSubscription config
    functionConfig.setCleanupSubscription(true);
    assertEquals(new Gson().toJson(functionConfig), new Gson().toJson(convertedConfig));
}
Also used : FunctionConfig(org.apache.pulsar.common.functions.FunctionConfig) IdentityFunction(org.apache.pulsar.functions.api.utils.IdentityFunction) Function(org.apache.pulsar.functions.proto.Function) HashMap(java.util.HashMap) IdentityFunction(org.apache.pulsar.functions.api.utils.IdentityFunction) FunctionDetails(org.apache.pulsar.functions.proto.Function.FunctionDetails) ProducerConfig(org.apache.pulsar.common.functions.ProducerConfig) Gson(com.google.gson.Gson) ConsumerConfig(org.apache.pulsar.common.functions.ConsumerConfig) Test(org.testng.annotations.Test)

Example 5 with ProducerConfig

use of org.apache.pulsar.common.functions.ProducerConfig in project pulsar by yahoo.

the class FunctionConfigUtils method convertFromDetails.

public static FunctionConfig convertFromDetails(FunctionDetails functionDetails) {
    FunctionConfig functionConfig = new FunctionConfig();
    functionConfig.setTenant(functionDetails.getTenant());
    functionConfig.setNamespace(functionDetails.getNamespace());
    functionConfig.setName(functionDetails.getName());
    functionConfig.setParallelism(functionDetails.getParallelism());
    functionConfig.setProcessingGuarantees(FunctionCommon.convertProcessingGuarantee(functionDetails.getProcessingGuarantees()));
    Map<String, ConsumerConfig> consumerConfigMap = new HashMap<>();
    for (Map.Entry<String, Function.ConsumerSpec> input : functionDetails.getSource().getInputSpecsMap().entrySet()) {
        ConsumerConfig consumerConfig = new ConsumerConfig();
        if (isNotEmpty(input.getValue().getSerdeClassName())) {
            consumerConfig.setSerdeClassName(input.getValue().getSerdeClassName());
        }
        if (isNotEmpty(input.getValue().getSchemaType())) {
            consumerConfig.setSchemaType(input.getValue().getSchemaType());
        }
        if (input.getValue().hasReceiverQueueSize()) {
            consumerConfig.setReceiverQueueSize(input.getValue().getReceiverQueueSize().getValue());
        }
        if (input.getValue().hasCryptoSpec()) {
            consumerConfig.setCryptoConfig(CryptoUtils.convertFromSpec(input.getValue().getCryptoSpec()));
        }
        consumerConfig.setRegexPattern(input.getValue().getIsRegexPattern());
        consumerConfig.setSchemaProperties(input.getValue().getSchemaPropertiesMap());
        consumerConfig.setPoolMessages(input.getValue().getPoolMessages());
        consumerConfigMap.put(input.getKey(), consumerConfig);
    }
    functionConfig.setInputSpecs(consumerConfigMap);
    if (!isEmpty(functionDetails.getSource().getSubscriptionName())) {
        functionConfig.setSubName(functionDetails.getSource().getSubscriptionName());
    }
    functionConfig.setRetainOrdering(functionDetails.getRetainOrdering());
    functionConfig.setRetainKeyOrdering(functionDetails.getRetainKeyOrdering());
    functionConfig.setCleanupSubscription(functionDetails.getSource().getCleanupSubscription());
    functionConfig.setAutoAck(functionDetails.getAutoAck());
    // Set subscription position
    functionConfig.setSubscriptionPosition(convertFromFunctionDetailsSubscriptionPosition(functionDetails.getSource().getSubscriptionPosition()));
    if (functionDetails.getSource().getTimeoutMs() != 0) {
        functionConfig.setTimeoutMs(functionDetails.getSource().getTimeoutMs());
    }
    if (!isEmpty(functionDetails.getSink().getTopic())) {
        functionConfig.setOutput(functionDetails.getSink().getTopic());
    }
    if (!isEmpty(functionDetails.getSink().getSerDeClassName())) {
        functionConfig.setOutputSerdeClassName(functionDetails.getSink().getSerDeClassName());
    }
    if (!isEmpty(functionDetails.getSink().getSchemaType())) {
        functionConfig.setOutputSchemaType(functionDetails.getSink().getSchemaType());
    }
    if (functionDetails.getSink().getProducerSpec() != null) {
        Function.ProducerSpec spec = functionDetails.getSink().getProducerSpec();
        ProducerConfig producerConfig = new ProducerConfig();
        if (spec.getMaxPendingMessages() != 0) {
            producerConfig.setMaxPendingMessages(spec.getMaxPendingMessages());
        }
        if (spec.getMaxPendingMessagesAcrossPartitions() != 0) {
            producerConfig.setMaxPendingMessagesAcrossPartitions(spec.getMaxPendingMessagesAcrossPartitions());
        }
        if (spec.hasCryptoSpec()) {
            producerConfig.setCryptoConfig(CryptoUtils.convertFromSpec(spec.getCryptoSpec()));
        }
        if (spec.getBatchBuilder() != null) {
            producerConfig.setBatchBuilder(spec.getBatchBuilder());
        }
        producerConfig.setUseThreadLocalProducers(spec.getUseThreadLocalProducers());
        functionConfig.setProducerConfig(producerConfig);
    }
    if (!isEmpty(functionDetails.getLogTopic())) {
        functionConfig.setLogTopic(functionDetails.getLogTopic());
    }
    if (functionDetails.getSink().getForwardSourceMessageProperty()) {
        functionConfig.setForwardSourceMessageProperty(functionDetails.getSink().getForwardSourceMessageProperty());
    }
    functionConfig.setRuntime(FunctionCommon.convertRuntime(functionDetails.getRuntime()));
    if (functionDetails.hasRetryDetails()) {
        functionConfig.setMaxMessageRetries(functionDetails.getRetryDetails().getMaxMessageRetries());
        if (!isEmpty(functionDetails.getRetryDetails().getDeadLetterTopic())) {
            functionConfig.setDeadLetterTopic(functionDetails.getRetryDetails().getDeadLetterTopic());
        }
    }
    Map<String, Object> userConfig;
    if (!isEmpty(functionDetails.getUserConfig())) {
        Type type = new TypeToken<Map<String, Object>>() {
        }.getType();
        userConfig = new Gson().fromJson(functionDetails.getUserConfig(), type);
    } else {
        userConfig = new HashMap<>();
    }
    if (userConfig.containsKey(WindowConfig.WINDOW_CONFIG_KEY)) {
        WindowConfig windowConfig = new Gson().fromJson((new Gson().toJson(userConfig.get(WindowConfig.WINDOW_CONFIG_KEY))), WindowConfig.class);
        userConfig.remove(WindowConfig.WINDOW_CONFIG_KEY);
        functionConfig.setClassName(windowConfig.getActualWindowFunctionClassName());
        functionConfig.setWindowConfig(windowConfig);
    } else {
        functionConfig.setClassName(functionDetails.getClassName());
    }
    functionConfig.setUserConfig(userConfig);
    if (!isEmpty(functionDetails.getSecretsMap())) {
        Type type = new TypeToken<Map<String, Object>>() {
        }.getType();
        Map<String, Object> secretsMap = new Gson().fromJson(functionDetails.getSecretsMap(), type);
        functionConfig.setSecrets(secretsMap);
    }
    if (functionDetails.hasResources()) {
        Resources resources = new Resources();
        resources.setCpu(functionDetails.getResources().getCpu());
        resources.setRam(functionDetails.getResources().getRam());
        resources.setDisk(functionDetails.getResources().getDisk());
        functionConfig.setResources(resources);
    }
    if (!isEmpty(functionDetails.getRuntimeFlags())) {
        functionConfig.setRuntimeFlags(functionDetails.getRuntimeFlags());
    }
    if (!isEmpty(functionDetails.getCustomRuntimeOptions())) {
        functionConfig.setCustomRuntimeOptions(functionDetails.getCustomRuntimeOptions());
    }
    return functionConfig;
}
Also used : FunctionConfig(org.apache.pulsar.common.functions.FunctionConfig) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) WindowConfig(org.apache.pulsar.common.functions.WindowConfig) Function(org.apache.pulsar.functions.proto.Function) Type(java.lang.reflect.Type) ProducerConfig(org.apache.pulsar.common.functions.ProducerConfig) ConsumerConfig(org.apache.pulsar.common.functions.ConsumerConfig) Resources(org.apache.pulsar.common.functions.Resources) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

HashMap (java.util.HashMap)21 ProducerConfig (org.apache.pulsar.common.functions.ProducerConfig)21 Gson (com.google.gson.Gson)18 Function (org.apache.pulsar.functions.proto.Function)15 ConsumerConfig (org.apache.pulsar.common.functions.ConsumerConfig)12 FunctionConfig (org.apache.pulsar.common.functions.FunctionConfig)12 Resources (org.apache.pulsar.common.functions.Resources)12 IdentityFunction (org.apache.pulsar.functions.api.utils.IdentityFunction)12 FunctionDetails (org.apache.pulsar.functions.proto.Function.FunctionDetails)12 Type (java.lang.reflect.Type)9 Map (java.util.Map)9 WindowConfig (org.apache.pulsar.common.functions.WindowConfig)9 BatchSourceConfig (org.apache.pulsar.common.io.BatchSourceConfig)6 SourceConfig (org.apache.pulsar.common.io.SourceConfig)6 Test (org.testng.annotations.Test)6 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 TypeToken (com.google.gson.reflect.TypeToken)3 File (java.io.File)3 MalformedURLException (java.net.MalformedURLException)3