Search in sources :

Example 1 with FunctionDetails

use of org.apache.pulsar.functions.proto.Function.FunctionDetails in project pulsar by apache.

the class FunctionActioner method terminateFunction.

public void terminateFunction(FunctionRuntimeInfo functionRuntimeInfo) {
    FunctionDetails details = functionRuntimeInfo.getFunctionInstance().getFunctionMetaData().getFunctionDetails();
    String fqfn = FunctionCommon.getFullyQualifiedName(details);
    log.info("{}-{} Terminating function...", fqfn, functionRuntimeInfo.getFunctionInstance().getInstanceId());
    if (functionRuntimeInfo.getRuntimeSpawner() != null) {
        functionRuntimeInfo.getRuntimeSpawner().close();
        // cleanup any auth data cached
        if (workerConfig.isAuthenticationEnabled()) {
            functionRuntimeInfo.getRuntimeSpawner().getRuntimeFactory().getAuthProvider().ifPresent(functionAuthProvider -> {
                try {
                    log.info("{}-{} Cleaning up authentication data for function...", fqfn, functionRuntimeInfo.getFunctionInstance().getInstanceId());
                    functionAuthProvider.cleanUpAuthData(details, Optional.ofNullable(getFunctionAuthData(Optional.ofNullable(functionRuntimeInfo.getRuntimeSpawner().getInstanceConfig().getFunctionAuthenticationSpec()))));
                } catch (Exception e) {
                    log.error("Failed to cleanup auth data for function: {}", fqfn, e);
                }
            });
        }
        functionRuntimeInfo.setRuntimeSpawner(null);
    }
    cleanupFunctionFiles(functionRuntimeInfo);
    // cleanup subscriptions
    if (details.getSource().getCleanupSubscription()) {
        Map<String, Function.ConsumerSpec> consumerSpecMap = details.getSource().getInputSpecsMap();
        consumerSpecMap.entrySet().forEach(new Consumer<Map.Entry<String, Function.ConsumerSpec>>() {

            @Override
            public void accept(Map.Entry<String, Function.ConsumerSpec> stringConsumerSpecEntry) {
                Function.ConsumerSpec consumerSpec = stringConsumerSpecEntry.getValue();
                String topic = stringConsumerSpecEntry.getKey();
                String subscriptionName = isBlank(functionRuntimeInfo.getFunctionInstance().getFunctionMetaData().getFunctionDetails().getSource().getSubscriptionName()) ? InstanceUtils.getDefaultSubscriptionName(functionRuntimeInfo.getFunctionInstance().getFunctionMetaData().getFunctionDetails()) : functionRuntimeInfo.getFunctionInstance().getFunctionMetaData().getFunctionDetails().getSource().getSubscriptionName();
                deleteSubscription(topic, consumerSpec, subscriptionName, String.format("Cleaning up subscriptions for function %s", fqfn));
            }
        });
    }
    // clean up done for batch sources if necessary
    cleanupBatchSource(details);
}
Also used : FunctionDetails(org.apache.pulsar.functions.proto.Function.FunctionDetails) Function(org.apache.pulsar.functions.proto.Function) Map(java.util.Map) FileNotFoundException(java.io.FileNotFoundException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException)

Example 2 with FunctionDetails

use of org.apache.pulsar.functions.proto.Function.FunctionDetails in project pulsar by apache.

the class FunctionActioner method stopFunction.

public void stopFunction(FunctionRuntimeInfo functionRuntimeInfo) {
    Function.Instance instance = functionRuntimeInfo.getFunctionInstance();
    FunctionMetaData functionMetaData = instance.getFunctionMetaData();
    FunctionDetails details = functionMetaData.getFunctionDetails();
    log.info("{}/{}/{}-{} Stopping function...", details.getTenant(), details.getNamespace(), details.getName(), instance.getInstanceId());
    if (functionRuntimeInfo.getRuntimeSpawner() != null) {
        functionRuntimeInfo.getRuntimeSpawner().close();
        functionRuntimeInfo.setRuntimeSpawner(null);
    }
    cleanupFunctionFiles(functionRuntimeInfo);
}
Also used : FunctionMetaData(org.apache.pulsar.functions.proto.Function.FunctionMetaData) Function(org.apache.pulsar.functions.proto.Function) FunctionDetails(org.apache.pulsar.functions.proto.Function.FunctionDetails)

Example 3 with FunctionDetails

use of org.apache.pulsar.functions.proto.Function.FunctionDetails in project pulsar by apache.

the class FunctionConfigUtilsTest method testFunctionConfigConvertFromDetails.

@Test
public void testFunctionConfigConvertFromDetails() {
    String name = "test1";
    String namespace = "ns1";
    String tenant = "tenant1";
    String classname = getClass().getName();
    int parallelism = 3;
    Map<String, String> userConfig = new HashMap<>();
    userConfig.put("key1", "val1");
    Function.ProcessingGuarantees processingGuarantees = Function.ProcessingGuarantees.EFFECTIVELY_ONCE;
    Function.FunctionDetails.Runtime runtime = Function.FunctionDetails.Runtime.JAVA;
    Function.SinkSpec sinkSpec = Function.SinkSpec.newBuilder().setTopic("sinkTopic1").build();
    Map<String, Function.ConsumerSpec> consumerSpecMap = new HashMap<>();
    consumerSpecMap.put("sourceTopic1", Function.ConsumerSpec.newBuilder().setSchemaType(JSONSchema.class.getName()).build());
    Function.SourceSpec sourceSpec = Function.SourceSpec.newBuilder().putAllInputSpecs(consumerSpecMap).setSubscriptionType(Function.SubscriptionType.FAILOVER).setCleanupSubscription(true).build();
    boolean autoAck = true;
    String logTopic = "log-topic1";
    Function.Resources resources = Function.Resources.newBuilder().setCpu(1.5).setDisk(1024 * 20).setRam(1024 * 10).build();
    String packageUrl = "http://package.url";
    Map<String, String> secretsMap = new HashMap<>();
    secretsMap.put("secretConfigKey1", "secretConfigVal1");
    Function.RetryDetails retryDetails = Function.RetryDetails.newBuilder().setDeadLetterTopic("dead-letter-1").build();
    Function.FunctionDetails functionDetails = Function.FunctionDetails.newBuilder().setNamespace(namespace).setTenant(tenant).setName(name).setClassName(classname).setParallelism(parallelism).setUserConfig(new Gson().toJson(userConfig)).setProcessingGuarantees(processingGuarantees).setRuntime(runtime).setSink(sinkSpec).setSource(sourceSpec).setAutoAck(autoAck).setLogTopic(logTopic).setResources(resources).setPackageUrl(packageUrl).setSecretsMap(new Gson().toJson(secretsMap)).setRetryDetails(retryDetails).build();
    FunctionConfig functionConfig = FunctionConfigUtils.convertFromDetails(functionDetails);
    assertEquals(functionConfig.getTenant(), tenant);
    assertEquals(functionConfig.getNamespace(), namespace);
    assertEquals(functionConfig.getName(), name);
    assertEquals(functionConfig.getClassName(), classname);
    assertEquals(functionConfig.getLogTopic(), logTopic);
    assertEquals((Object) functionConfig.getResources().getCpu(), resources.getCpu());
    assertEquals(functionConfig.getResources().getDisk().longValue(), resources.getDisk());
    assertEquals(functionConfig.getResources().getRam().longValue(), resources.getRam());
    assertEquals(functionConfig.getOutput(), sinkSpec.getTopic());
    assertEquals(functionConfig.getInputSpecs().keySet(), sourceSpec.getInputSpecsMap().keySet());
    assertEquals(functionConfig.getCleanupSubscription().booleanValue(), sourceSpec.getCleanupSubscription());
}
Also used : FunctionConfig(org.apache.pulsar.common.functions.FunctionConfig) HashMap(java.util.HashMap) FunctionDetails(org.apache.pulsar.functions.proto.Function.FunctionDetails) Gson(com.google.gson.Gson) IdentityFunction(org.apache.pulsar.functions.api.utils.IdentityFunction) Function(org.apache.pulsar.functions.proto.Function) FunctionDetails(org.apache.pulsar.functions.proto.Function.FunctionDetails) JSONSchema(org.apache.pulsar.client.impl.schema.JSONSchema) Test(org.testng.annotations.Test)

Example 4 with FunctionDetails

use of org.apache.pulsar.functions.proto.Function.FunctionDetails 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 5 with FunctionDetails

use of org.apache.pulsar.functions.proto.Function.FunctionDetails in project pulsar by apache.

the class ContextImplTest method setup.

@BeforeMethod
public void setup() throws PulsarClientException {
    config = new InstanceConfig();
    config.setExposePulsarAdminClientEnabled(true);
    FunctionDetails functionDetails = FunctionDetails.newBuilder().setUserConfig("").build();
    config.setFunctionDetails(functionDetails);
    logger = mock(Logger.class);
    pulsarAdmin = mock(PulsarAdmin.class);
    client = mock(PulsarClientImpl.class);
    when(client.newProducer()).thenReturn(new ProducerBuilderImpl(client, Schema.BYTES));
    when(client.createProducerAsync(any(ProducerConfigurationData.class), any(), any())).thenReturn(CompletableFuture.completedFuture(producer));
    when(client.getSchema(anyString())).thenReturn(CompletableFuture.completedFuture(Optional.empty()));
    when(producer.sendAsync(anyString())).thenReturn(CompletableFuture.completedFuture(null));
    clientBuilder = mock(ClientBuilder.class);
    when(clientBuilder.build()).thenReturn(client);
    TypedMessageBuilder messageBuilder = spy(new TypedMessageBuilderImpl(mock(ProducerBase.class), Schema.STRING));
    doReturn(new CompletableFuture<>()).when(messageBuilder).sendAsync();
    when(producer.newMessage()).thenReturn(messageBuilder);
    context = new ContextImpl(config, logger, client, new EnvironmentBasedSecretsProvider(), FunctionCollectorRegistry.getDefaultImplementation(), new String[0], FunctionDetails.ComponentType.FUNCTION, null, new InstanceStateManager(), pulsarAdmin, clientBuilder);
    context.setCurrentMessageContext((Record<String>) () -> null);
}
Also used : PulsarAdmin(org.apache.pulsar.client.admin.PulsarAdmin) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Logger(org.slf4j.Logger) ProducerConfigurationData(org.apache.pulsar.client.impl.conf.ProducerConfigurationData) FunctionDetails(org.apache.pulsar.functions.proto.Function.FunctionDetails) TypedMessageBuilder(org.apache.pulsar.client.api.TypedMessageBuilder) TypedMessageBuilderImpl(org.apache.pulsar.client.impl.TypedMessageBuilderImpl) ProducerBuilderImpl(org.apache.pulsar.client.impl.ProducerBuilderImpl) InstanceStateManager(org.apache.pulsar.functions.instance.state.InstanceStateManager) EnvironmentBasedSecretsProvider(org.apache.pulsar.functions.secretsprovider.EnvironmentBasedSecretsProvider) PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) ClientBuilder(org.apache.pulsar.client.api.ClientBuilder) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

FunctionDetails (org.apache.pulsar.functions.proto.Function.FunctionDetails)111 Test (org.testng.annotations.Test)42 IdentityFunction (org.apache.pulsar.functions.api.utils.IdentityFunction)27 Function (org.apache.pulsar.functions.proto.Function)27 HashMap (java.util.HashMap)21 FunctionConfig (org.apache.pulsar.common.functions.FunctionConfig)21 Gson (com.google.gson.Gson)18 FunctionMetaData (org.apache.pulsar.functions.proto.Function.FunctionMetaData)18 ConsumerConfig (org.apache.pulsar.common.functions.ConsumerConfig)15 File (java.io.File)12 ProducerConfig (org.apache.pulsar.common.functions.ProducerConfig)12 Resources (org.apache.pulsar.common.functions.Resources)12 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)9 IOException (java.io.IOException)9 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)9 SinkSpec (org.apache.pulsar.functions.proto.Function.SinkSpec)9 URL (java.net.URL)6 Map (java.util.Map)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 FileNotFoundException (java.io.FileNotFoundException)5