Search in sources :

Example 1 with DefaultSerDe

use of org.apache.pulsar.functions.api.utils.DefaultSerDe in project incubator-pulsar by apache.

the class CmdFunctionsTest method setup.

@BeforeMethod
public void setup() throws Exception {
    this.admin = mock(PulsarAdminWithFunctions.class);
    this.functions = mock(Functions.class);
    when(admin.functions()).thenReturn(functions);
    when(admin.getServiceUrl()).thenReturn(URI.create("http://localhost:1234").toURL());
    when(admin.getClientConf()).thenReturn(new ClientConfigurationData());
    this.cmd = new CmdFunctions(admin);
    // mock reflections
    mockStatic(Reflections.class);
    when(Reflections.classExistsInJar(any(File.class), anyString())).thenReturn(true);
    when(Reflections.classExists(anyString())).thenReturn(true);
    when(Reflections.classInJarImplementsIface(any(File.class), anyString(), eq(Function.class))).thenReturn(true);
    when(Reflections.classImplementsIface(anyString(), any())).thenReturn(true);
    when(Reflections.createInstance(eq(DummyFunction.class.getName()), any(File.class))).thenReturn(new DummyFunction());
    when(Reflections.createInstance(eq(DefaultSerDe.class.getName()), any(File.class))).thenReturn(new DefaultSerDe(String.class));
}
Also used : ClientConfigurationData(org.apache.pulsar.client.impl.conf.ClientConfigurationData) PulsarAdminWithFunctions(org.apache.pulsar.client.admin.PulsarAdminWithFunctions) DeleteFunction(org.apache.pulsar.admin.cli.CmdFunctions.DeleteFunction) GetFunction(org.apache.pulsar.admin.cli.CmdFunctions.GetFunction) UpdateFunction(org.apache.pulsar.admin.cli.CmdFunctions.UpdateFunction) CreateFunction(org.apache.pulsar.admin.cli.CmdFunctions.CreateFunction) Function(org.apache.pulsar.functions.api.Function) DefaultSerDe(org.apache.pulsar.functions.api.utils.DefaultSerDe) PulsarAdminWithFunctions(org.apache.pulsar.client.admin.PulsarAdminWithFunctions) Functions(org.apache.pulsar.client.admin.Functions) ListFunctions(org.apache.pulsar.admin.cli.CmdFunctions.ListFunctions) Matchers.anyString(org.mockito.Matchers.anyString) File(java.io.File) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 2 with DefaultSerDe

use of org.apache.pulsar.functions.api.utils.DefaultSerDe in project incubator-pulsar by apache.

the class JavaInstanceRunnableProcessTest method testSetupJavaInstance.

/**
 * Test the basic run logic of instance.
 */
@Test
public void testSetupJavaInstance() throws Exception {
    JavaInstanceRunnable runnable = new JavaInstanceRunnable(config, fnCache, "test-jar-file", mockClient, TEST_STORAGE_SERVICE_URL);
    runnable.setupJavaInstance();
    // verify
    // 1. verify jar is loaded
    verify(fnCache, times(1)).registerFunctionInstance(eq(config.getFunctionId()), eq(config.getInstanceId()), eq(Arrays.asList("test-jar-file")), eq(Collections.emptyList()));
    verify(fnCache, times(1)).getClassLoader(eq(config.getFunctionId()));
    // 2. verify serde is setup
    for (String inputTopic : fnConfig.getInputsList()) {
        assertTrue(runnable.getInputSerDe().containsKey(inputTopic));
        assertTrue(runnable.getInputSerDe().get(inputTopic) instanceof DefaultSerDe);
        DefaultSerDe serDe = (DefaultSerDe) runnable.getInputSerDe().get(inputTopic);
        assertEquals(String.class, Whitebox.getInternalState(serDe, "type"));
    }
    // 3. verify producers and consumers are setup
    Producers producers = runnable.getOutputProducer();
    assertTrue(producers instanceof SimpleOneOuputTopicProducers);
    assertSame(mockProducers.get(Pair.of(fnConfig.getOutput(), null)).getProducer(), ((SimpleOneOuputTopicProducers) producers).getProducer());
    assertEquals(mockConsumers.size(), runnable.getInputConsumers().size());
    for (Map.Entry<String, Consumer> consumerEntry : runnable.getInputConsumers().entrySet()) {
        String topic = consumerEntry.getKey();
        Consumer mockConsumer = mockConsumers.get(Pair.of(topic, FunctionConfigUtils.getFullyQualifiedName(fnConfig))).getConsumer();
        assertSame(mockConsumer, consumerEntry.getValue());
    }
    // 4. verify state table
    assertSame(mockStorageClient, runnable.getStorageClient());
    assertSame(mockTable, runnable.getStateTable());
    runnable.close();
    // verify close
    for (ConsumerInstance consumer : mockConsumers.values()) {
        verify(consumer.getConsumer(), times(1)).close();
    }
    assertTrue(runnable.getInputConsumers().isEmpty());
    for (ProducerInstance producer : mockProducers.values()) {
        verify(producer.getProducer(), times(1)).close();
    }
    verify(mockTable, times(1)).close();
    verify(mockStorageClient, times(1)).close();
    // function is unregistered
    verify(fnCache, times(1)).unregisterFunctionInstance(eq(config.getFunctionId()), eq(config.getInstanceId()));
}
Also used : Producers(org.apache.pulsar.functions.instance.producers.Producers) SimpleOneOuputTopicProducers(org.apache.pulsar.functions.instance.producers.SimpleOneOuputTopicProducers) Consumer(org.apache.pulsar.client.api.Consumer) DefaultSerDe(org.apache.pulsar.functions.api.utils.DefaultSerDe) Matchers.anyString(org.mockito.Matchers.anyString) SimpleOneOuputTopicProducers(org.apache.pulsar.functions.instance.producers.SimpleOneOuputTopicProducers) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with DefaultSerDe

use of org.apache.pulsar.functions.api.utils.DefaultSerDe in project incubator-pulsar by apache.

the class ContextImpl method publish.

@Override
public <O> CompletableFuture<Void> publish(String topicName, O object, String serDeClassName) {
    if (!publishProducers.containsKey(topicName)) {
        try {
            publishProducers.put(topicName, pulsarClient.createProducer(topicName, producerConfiguration));
        } catch (PulsarClientException ex) {
            CompletableFuture<Void> retval = new CompletableFuture<>();
            retval.completeExceptionally(ex);
            return retval;
        }
    }
    if (!publishSerializers.containsKey(serDeClassName)) {
        SerDe serDe;
        if (serDeClassName.equals(DefaultSerDe.class.getName())) {
            if (!DefaultSerDe.IsSupportedType(object.getClass())) {
                throw new RuntimeException("Default Serializer does not support " + object.getClass());
            }
            serDe = new DefaultSerDe(object.getClass());
        } else {
            try {
                Class<? extends SerDe> serDeClass = (Class<? extends SerDe>) Class.forName(serDeClassName);
                serDe = Reflections.createInstance(serDeClassName, serDeClass, classLoader);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
        publishSerializers.put(serDeClassName, serDe);
    }
    byte[] bytes = publishSerializers.get(serDeClassName).serialize(object);
    return publishProducers.get(topicName).sendAsync(bytes).thenApply(msgId -> null);
}
Also used : DefaultSerDe(org.apache.pulsar.functions.api.utils.DefaultSerDe) SerDe(org.apache.pulsar.functions.api.SerDe) CompletableFuture(java.util.concurrent.CompletableFuture) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) DefaultSerDe(org.apache.pulsar.functions.api.utils.DefaultSerDe)

Aggregations

DefaultSerDe (org.apache.pulsar.functions.api.utils.DefaultSerDe)3 Matchers.anyString (org.mockito.Matchers.anyString)2 File (java.io.File)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CreateFunction (org.apache.pulsar.admin.cli.CmdFunctions.CreateFunction)1 DeleteFunction (org.apache.pulsar.admin.cli.CmdFunctions.DeleteFunction)1 GetFunction (org.apache.pulsar.admin.cli.CmdFunctions.GetFunction)1 ListFunctions (org.apache.pulsar.admin.cli.CmdFunctions.ListFunctions)1 UpdateFunction (org.apache.pulsar.admin.cli.CmdFunctions.UpdateFunction)1 Functions (org.apache.pulsar.client.admin.Functions)1 PulsarAdminWithFunctions (org.apache.pulsar.client.admin.PulsarAdminWithFunctions)1 Consumer (org.apache.pulsar.client.api.Consumer)1 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)1 ClientConfigurationData (org.apache.pulsar.client.impl.conf.ClientConfigurationData)1 Function (org.apache.pulsar.functions.api.Function)1 SerDe (org.apache.pulsar.functions.api.SerDe)1 Producers (org.apache.pulsar.functions.instance.producers.Producers)1