Search in sources :

Example 1 with Function

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

the class JavaInstanceRunnableTest method testExplicitDefaultSerDe.

/**
 * Verify that Explicit setting of Default Serializer works fine.
 */
@Test
public void testExplicitDefaultSerDe() {
    try {
        JavaInstanceRunnable runnable = createRunnable(false, DefaultSerDe.class.getName());
        Method method = makeAccessible(runnable);
        ClassLoader clsLoader = Thread.currentThread().getContextClassLoader();
        Function function = (Function<String, String>) (input, context) -> input + "-lambda";
        Class<?>[] typeArgs = TypeResolver.resolveRawArguments(Function.class, function.getClass());
        method.invoke(runnable, typeArgs, clsLoader);
    } catch (Exception ex) {
        assertTrue(false);
    }
}
Also used : Function(org.apache.pulsar.functions.api.Function) DefaultSerDe(org.apache.pulsar.functions.api.utils.DefaultSerDe) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.testng.annotations.Test)

Example 2 with Function

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

the class JavaInstanceRunnableTest method testDefaultSerDe.

/**
 * Verify that Default Serializer works fine.
 */
@Test
public void testDefaultSerDe() {
    try {
        JavaInstanceRunnable runnable = createRunnable(false, null);
        Method method = makeAccessible(runnable);
        ClassLoader clsLoader = Thread.currentThread().getContextClassLoader();
        Function function = (Function<String, String>) (input, context) -> input + "-lambda";
        Class<?>[] typeArgs = TypeResolver.resolveRawArguments(Function.class, function.getClass());
        method.invoke(runnable, typeArgs, clsLoader);
    } catch (Exception ex) {
        ex.printStackTrace();
        assertEquals(ex, null);
        assertTrue(false);
    }
}
Also used : Function(org.apache.pulsar.functions.api.Function) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.testng.annotations.Test)

Example 3 with Function

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

the class JavaInstanceRunnableTest method testInconsistentOutputType.

/**
 * Verify that function output type should be consistent with output serde type.
 */
@Test
public void testInconsistentOutputType() {
    try {
        JavaInstanceRunnable runnable = createRunnable(false, IntegerSerDe.class.getName());
        Method method = makeAccessible(runnable);
        ClassLoader clsLoader = Thread.currentThread().getContextClassLoader();
        Function function = (Function<String, String>) (input, context) -> input + "-lambda";
        Class<?>[] typeArgs = TypeResolver.resolveRawArguments(Function.class, function.getClass());
        method.invoke(runnable, typeArgs, clsLoader);
        fail("Should fail constructing java instance if function type is inconsistent with serde type");
    } catch (InvocationTargetException ex) {
        assertTrue(ex.getCause().getMessage().startsWith("Inconsistent types found between function output type and output serde type:"));
    } catch (Exception ex) {
        assertTrue(false);
    }
}
Also used : Function(org.apache.pulsar.functions.api.Function) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.testng.annotations.Test)

Example 4 with Function

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

the class JavaInstanceRunnableTest method testInconsistentInputType.

/**
 * Verify that function input type should be consistent with input serde type.
 */
@Test
public void testInconsistentInputType() {
    try {
        JavaInstanceRunnable runnable = createRunnable(true, DefaultSerDe.class.getName());
        Method method = makeAccessible(runnable);
        ClassLoader clsLoader = Thread.currentThread().getContextClassLoader();
        Function function = (Function<String, String>) (input, context) -> input + "-lambda";
        Class<?>[] typeArgs = TypeResolver.resolveRawArguments(Function.class, function.getClass());
        method.invoke(runnable, typeArgs, clsLoader);
        fail("Should fail constructing java instance if function type is inconsistent with serde type");
    } catch (InvocationTargetException ex) {
        assertTrue(ex.getCause().getMessage().startsWith("Inconsistent types found between function input type and input serde type:"));
    } catch (Exception ex) {
        assertTrue(false);
    }
}
Also used : Function(org.apache.pulsar.functions.api.Function) DefaultSerDe(org.apache.pulsar.functions.api.utils.DefaultSerDe) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.testng.annotations.Test)

Example 5 with Function

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

the class JavaInstanceRunnable method setupJavaInstance.

/**
 * NOTE: this method should be called in the instance thread, in order to make class loading work.
 */
JavaInstance setupJavaInstance() throws Exception {
    // initialize the thread context
    ThreadContext.put("function", FunctionConfigUtils.getFullyQualifiedName(instanceConfig.getFunctionConfig()));
    ThreadContext.put("instance", instanceConfig.getInstanceId());
    log.info("Starting Java Instance {}", instanceConfig.getFunctionConfig().getName());
    // start the function thread
    loadJars();
    ClassLoader clsLoader = Thread.currentThread().getContextClassLoader();
    Object object = Reflections.createInstance(instanceConfig.getFunctionConfig().getClassName(), clsLoader);
    if (!(object instanceof Function) && !(object instanceof java.util.function.Function)) {
        throw new RuntimeException("User class must either be Function or java.util.Function");
    }
    Class<?>[] typeArgs;
    if (object instanceof Function) {
        Function function = (Function) object;
        typeArgs = TypeResolver.resolveRawArguments(Function.class, function.getClass());
    } else {
        java.util.function.Function function = (java.util.function.Function) object;
        typeArgs = TypeResolver.resolveRawArguments(java.util.function.Function.class, function.getClass());
    }
    // setup serde
    setupSerDe(typeArgs, clsLoader);
    // start the state table
    setupStateTable();
    // start the output producer
    startOutputProducer();
    // start the input consumer
    startInputConsumer();
    // start any log topic handler
    setupLogHandler();
    return new JavaInstance(instanceConfig, object, clsLoader, client, inputConsumers);
}
Also used : Function(org.apache.pulsar.functions.api.Function) java.util(java.util)

Aggregations

Function (org.apache.pulsar.functions.api.Function)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 Method (java.lang.reflect.Method)4 Test (org.testng.annotations.Test)4 DefaultSerDe (org.apache.pulsar.functions.api.utils.DefaultSerDe)2 java.util (java.util)1