Search in sources :

Example 1 with ComponentManager

use of org.talend.sdk.component.runtime.manager.ComponentManager in project component-runtime by Talend.

the class BeamJobTest method complex.

@Test
void complex(final TestInfo info, final TemporaryFolder temporaryFolder) throws IOException {
    final String testName = info.getTestMethod().get().getName();
    final String plugin = testName + ".jar";
    final File jar = pluginGenerator.createChainPlugin(temporaryFolder.getRoot(), plugin);
    final File out = new File(temporaryFolder.getRoot(), testName + "-out.txt");
    try (final ComponentManager manager = new ComponentManager(new File("target/fake-m2"), "TALEND-INF/dependencies.txt", null) {

        private final ComponentManager contextualInstance;

        {
            contextualInstance = CONTEXTUAL_INSTANCE.get();
            CONTEXTUAL_INSTANCE.set(this);
            addPlugin(jar.getAbsolutePath());
        }

        @Override
        public void close() {
            CONTEXTUAL_INSTANCE.set(contextualInstance);
            super.close();
            removePlugin(jar.getAbsolutePath());
        }
    }) {
        Job.components().component("formatter", "chain://formatter?__version=1").component("concat", "chain://concat?__version=1").component("concat_2", "chain://concat?__version=1").component("firstNames-dataset", "chain://list?__version=1" + "&values[0]=noha" + "&values[1]=Emma" + "&values[2]=liam" + "&values[3]=Olivia").component("lastNames-dataset", "chain://list?__version=1" + "&values[0]=manson" + "&values[1]=Sophia" + "&values[2]=jacob").component("address", "chain://list?__version=1" + "&values[0]=Paris" + "&values[1]=Strasbourg" + "&values[2]=Bordeaux" + "&values[3]=Nantes").component("outFile", "chain://file?__version=1&file=" + encode(out.getAbsolutePath(), "utf-8")).connections().from("firstNames-dataset").to("formatter", "firstName").from("lastNames-dataset").to("formatter", "lastName").from("formatter", "formatted-lastName").to("concat", "str1").from("formatter", "formatted-firstName").to("concat", "str2").from("concat").to("concat_2", "str1").from("address").to("concat_2", "str2").from("concat_2").to("outFile").build().property(GroupKeyProvider.class.getName(), (GroupKeyProvider) (context) -> context.getData().getJsonObject("$$internal").getString("key")).run();
        final int maxRetries = 120;
        for (int i = 0; i < maxRetries; i++) {
            // https://github.com/apache/beam/pull/4372
            try {
                assertTrue(out.isFile());
                assertEquals(Stream.of("MANSON noha Paris", "SOPHIA emma Strasbourg", "JACOB liam Bordeaux", "null olivia Nantes").collect(toSet()), new HashSet<>(Files.readAllLines(out.toPath())));
            } catch (final AssertionError ae) {
                if (i == maxRetries - 1) {
                    throw ae;
                }
                try {
                    sleep(500);
                } catch (final InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            }
        }
    }
}
Also used : ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) File(java.io.File) GroupKeyProvider(org.talend.sdk.component.runtime.manager.chain.GroupKeyProvider) Test(org.junit.jupiter.api.Test)

Example 2 with ComponentManager

use of org.talend.sdk.component.runtime.manager.ComponentManager in project component-runtime by Talend.

the class JobTest method jobKeyProvider.

@Test
void jobKeyProvider(final TestInfo info, final TemporaryFolder temporaryFolder) throws IOException {
    final String testName = info.getTestMethod().get().getName();
    final String plugin = testName + ".jar";
    final File jar = pluginGenerator.createChainPlugin(temporaryFolder.getRoot(), plugin);
    final File out = new File(temporaryFolder.getRoot(), testName + "-out.txt");
    try (final ComponentManager manager = new ComponentManager(new File("target/fake-m2"), "TALEND-INF/dependencies.txt", null) {

        {
            CONTEXTUAL_INSTANCE.set(this);
            addPlugin(jar.getAbsolutePath());
        }

        @Override
        public void close() {
            super.close();
            CONTEXTUAL_INSTANCE.set(null);
        }
    }) {
        Job.components().component("users", "db://input?__version=1&tableName=users").component("address", "db://input?__version=1&tableName=address").component("salary", "db://input?__version=1&tableName=salary").component("concat", "processor://concat?__version=1").component("concat_2", "processor://concat?__version=1").component("outFile", "file://out?__version=1&file=" + encode(out.getAbsolutePath(), "utf-8")).connections().from("users").to("concat", "str1").from("address").to("concat", "str2").from("concat").to("concat_2", "str1").from("salary").to("concat_2", "str2").from("concat_2").to("outFile").build().property(GroupKeyProvider.class.getName(), (GroupKeyProvider) context -> {
            if (context.getComponentId().equals("users")) {
                return context.getData().getString("id");
            }
            return context.getData().getString("userId");
        }).run();
        assertTrue(out.isFile());
        assertEquals(asList("emma strasbourg 1900", "sophia nantes 2000.5", "liam lyon 3055", "ava paris 2600.30"), Files.readAllLines(out.toPath()));
    }
}
Also used : ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 3 with ComponentManager

use of org.talend.sdk.component.runtime.manager.ComponentManager in project component-runtime by Talend.

the class JobTest method validateJobLifeCycle.

@Test
void validateJobLifeCycle(final TestInfo info, final TemporaryFolder temporaryFolder) {
    final String testName = info.getTestMethod().get().getName();
    final String plugin = testName + ".jar";
    final File jar = pluginGenerator.createChainPlugin(temporaryFolder.getRoot(), plugin);
    try (final ComponentManager manager = new ComponentManager(new File("target/fake-m2"), "TALEND-INF/dependencies.txt", null) {

        {
            CONTEXTUAL_INSTANCE.set(this);
            addPlugin(jar.getAbsolutePath());
        }

        @Override
        public void close() {
            super.close();
            CONTEXTUAL_INSTANCE.set(null);
        }
    }) {
        Job.components().component("countdown", "lifecycle://countdown?__version=1&start=2").component("square", "lifecycle://square?__version=1").connections().from("countdown").to("square").build().run();
        final LocalPartitionMapper mapper = LocalPartitionMapper.class.cast(manager.findMapper("lifecycle", "countdown", 1, emptyMap()).get());
        assertEquals(asList("start", "produce(1)", "produce(0)", "produce(null)", "stop"), ((Supplier<List<String>>) mapper.getDelegate()).get());
        final ProcessorImpl processor = (ProcessorImpl) manager.findProcessor("lifecycle", "square", 1, emptyMap()).get();
        assertEquals(asList("start", "beforeGroup", "onNext(1)", "afterGroup", "beforeGroup", "onNext(0)", "afterGroup", "stop"), ((Supplier<List<String>>) processor.getDelegate()).get());
    }
}
Also used : LocalPartitionMapper(org.talend.sdk.component.runtime.input.LocalPartitionMapper) ProcessorImpl(org.talend.sdk.component.runtime.output.ProcessorImpl) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) List(java.util.List) Arrays.asList(java.util.Arrays.asList) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 4 with ComponentManager

use of org.talend.sdk.component.runtime.manager.ComponentManager in project component-runtime by Talend.

the class InterceptorTest method run.

@Test
void run(final TemporaryFolder temporaryFolder) throws Exception {
    final File pluginFolder = new File(temporaryFolder.getRoot(), "test-plugins_" + UUID.randomUUID().toString());
    pluginFolder.mkdirs();
    final File plugin = pluginGenerator.createChainPlugin(pluginFolder, "plugin.jar");
    try (final ComponentManager manager = new ComponentManager(new File("target/test-dependencies"), "META-INF/test/dependencies", "org.talend.test:type=plugin,value=%s")) {
        manager.addPlugin(plugin.getAbsolutePath());
        final List<Object> collect = manager.find(c -> c.get(ComponentManager.AllServices.class).getServices().values().stream()).filter(c -> c.getClass().getName().endsWith("SuperService$$TalendServiceProxy")).collect(toList());
        assertEquals(1, collect.size());
        final Object instance = collect.iterator().next();
        final Method method = instance.getClass().getMethod("canBeLong", int.class);
        assertEquals("exec_1/1", method.invoke(instance, 1));
        assertEquals("exec_1/1", method.invoke(instance, 1));
        sleep(450);
        assertEquals("exec_1/2", method.invoke(instance, 1));
        assertEquals("exec_3/3", method.invoke(instance, 3));
        assertEquals("exec_4/4", method.invoke(instance, 4));
        final LightContainer container = manager.find(c -> Stream.of(c.get(LightContainer.class))).findFirst().get();
        DynamicContainerFinder.LOADERS.put("plugin", container.classloader());
        DynamicContainerFinder.SERVICES.put(instance.getClass().getSuperclass(), instance);
        final Object roundTrip = roundTrip(instance);
        assertEquals(roundTrip, instance);
    } finally {
        // clean temp files
        DynamicContainerFinder.LOADERS.clear();
        DynamicContainerFinder.SERVICES.clear();
    }
}
Also used : PluginGenerator(org.talend.sdk.component.runtime.manager.asm.PluginGenerator) UUID(java.util.UUID) Serializer.roundTrip(org.talend.sdk.component.runtime.manager.test.Serializer.roundTrip) LightContainer(org.talend.sdk.component.runtime.serialization.LightContainer) File(java.io.File) Test(org.junit.jupiter.api.Test) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Stream(java.util.stream.Stream) WithTemporaryFolder(org.talend.sdk.component.junit.base.junit5.WithTemporaryFolder) TemporaryFolder(org.talend.sdk.component.junit.base.junit5.TemporaryFolder) DynamicContainerFinder(org.talend.sdk.component.runtime.manager.serialization.DynamicContainerFinder) Thread.sleep(java.lang.Thread.sleep) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) Method(java.lang.reflect.Method) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) LightContainer(org.talend.sdk.component.runtime.serialization.LightContainer) Method(java.lang.reflect.Method) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 5 with ComponentManager

use of org.talend.sdk.component.runtime.manager.ComponentManager in project component-runtime by Talend.

the class StandaloneContainerFinder method find.

// IMPORTANT: don't abuse of lambdas here, it is on the runtime codepath
@Override
public LightContainer find(final String plugin) {
    final ComponentManager manager = ComponentManager.instance();
    Optional<Container> optionalContainer = manager.findPlugin(plugin);
    if (!optionalContainer.isPresent()) {
        log.info("Didn't find plugin " + plugin + ", had: " + manager.availablePlugins());
        // nested in current jar.
        try {
            optionalContainer = manager.findPlugin(manager.addPlugin(plugin));
        } catch (final IllegalArgumentException iae) {
            // concurrent request?
            optionalContainer = manager.findPlugin(plugin);
        }
    }
    if (optionalContainer.isPresent()) {
        final LightContainer lightContainer = optionalContainer.get().get(LightContainer.class);
        if (lightContainer != null) {
            return lightContainer;
        }
    }
    // TCCL
    return super.find(plugin);
}
Also used : Container(org.talend.sdk.component.container.Container) LightContainer(org.talend.sdk.component.runtime.serialization.LightContainer) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) LightContainer(org.talend.sdk.component.runtime.serialization.LightContainer)

Aggregations

ComponentManager (org.talend.sdk.component.runtime.manager.ComponentManager)21 Test (org.junit.jupiter.api.Test)12 File (java.io.File)8 JsonObject (javax.json.JsonObject)8 List (java.util.List)7 Collectors.toList (java.util.stream.Collectors.toList)7 Collection (java.util.Collection)6 HashMap (java.util.HashMap)6 Jsonb (javax.json.bind.Jsonb)6 AllArgsConstructor (lombok.AllArgsConstructor)6 Data (lombok.Data)6 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)6 Serializable (java.io.Serializable)5 ArrayList (java.util.ArrayList)5 Collections.singletonMap (java.util.Collections.singletonMap)5 Map (java.util.Map)5 Optional (java.util.Optional)5 PrimitiveIterator (java.util.PrimitiveIterator)5 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)5 IntStream (java.util.stream.IntStream)5