Search in sources :

Example 26 with JobSpec

use of org.apache.gobblin.runtime.api.JobSpec in project incubator-gobblin by apache.

the class TestFSJobCatalog method testCallbacks.

@Test
public void testCallbacks() throws Exception {
    this.jobConfigDir = java.nio.file.Files.createTempDirectory(String.format("gobblin-test_%s_job-conf", this.getClass().getSimpleName())).toFile();
    this.jobConfigDirPath = new Path(this.jobConfigDir.getPath());
    try (PrintWriter printWriter = new PrintWriter(new Path(jobConfigDirPath, "job3.template").toString(), "UTF-8")) {
        printWriter.println("param1 = value1");
        printWriter.println("param2 = value2");
    }
    Properties properties = new Properties();
    properties.setProperty(ConfigurationKeys.JOB_CONFIG_FILE_GENERAL_PATH_KEY, this.jobConfigDir.getPath());
    PathAlterationObserver observer = new PathAlterationObserver(this.jobConfigDirPath);
    /* Exposed the observer so that checkAndNotify can be manually invoked. */
    FSJobCatalog cat = new FSJobCatalog(ConfigUtils.propertiesToConfig(properties), observer);
    cat.startAsync();
    cat.awaitRunning(10, TimeUnit.SECONDS);
    final Map<URI, JobSpec> specs = new Hashtable<>();
    JobCatalogListener l = Mockito.mock(JobCatalogListener.class);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            JobSpec spec = (JobSpec) invocation.getArguments()[0];
            specs.put(spec.getUri(), spec);
            return null;
        }
    }).when(l).onAddJob(Mockito.any(JobSpec.class));
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            JobSpec spec = (JobSpec) invocation.getArguments()[0];
            specs.put(spec.getUri(), spec);
            return null;
        }
    }).when(l).onUpdateJob(Mockito.any(JobSpec.class));
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            URI uri = (URI) invocation.getArguments()[0];
            specs.remove(uri);
            return null;
        }
    }).when(l).onDeleteJob(Mockito.any(URI.class), Mockito.anyString());
    JobSpec js1_1 = JobSpec.builder("test_job1").withVersion("1").build();
    JobSpec js1_2 = JobSpec.builder("test_job1").withVersion("2").build();
    JobSpec js2 = JobSpec.builder("test_job2").withVersion("1").build();
    JobSpec js3 = JobSpec.builder("test_job3").withVersion("1").withTemplate(new URI("FS:///job3.template")).withConfig(ConfigBuilder.create().addPrimitive("job.template", "FS:///job3.template").build()).build();
    cat.addListener(l);
    observer.initialize();
    cat.put(js1_1);
    // enough time for file creation.
    observer.checkAndNotify();
    Assert.assertTrue(specs.containsKey(js1_1.getUri()));
    JobSpec js1_1_notified = specs.get(js1_1.getUri());
    Assert.assertTrue(ConfigUtils.verifySubset(js1_1_notified.getConfig(), js1_1.getConfig()));
    Assert.assertEquals(js1_1.getVersion(), js1_1_notified.getVersion());
    // Linux system has too large granularity for the modification time.
    Thread.sleep(1000);
    cat.put(js1_2);
    // enough time for file replacement.
    observer.checkAndNotify();
    Assert.assertTrue(specs.containsKey(js1_2.getUri()));
    JobSpec js1_2_notified = specs.get(js1_2.getUri());
    Assert.assertTrue(ConfigUtils.verifySubset(js1_2_notified.getConfig(), js1_2.getConfig()));
    Assert.assertEquals(js1_2.getVersion(), js1_2_notified.getVersion());
    Thread.sleep(1000);
    cat.put(js2);
    observer.checkAndNotify();
    Assert.assertTrue(specs.containsKey(js2.getUri()));
    JobSpec js2_notified = specs.get(js2.getUri());
    Assert.assertTrue(ConfigUtils.verifySubset(js2_notified.getConfig(), js2.getConfig()));
    Assert.assertEquals(js2.getVersion(), js2_notified.getVersion());
    Thread.sleep(1000);
    cat.remove(js2.getUri());
    // enough time for file deletion.
    observer.checkAndNotify();
    Assert.assertFalse(specs.containsKey(js2.getUri()));
    Thread.sleep(1000);
    cat.put(js3);
    observer.checkAndNotify();
    Assert.assertTrue(specs.containsKey(js3.getUri()));
    JobSpec js3_notified = specs.get(js3.getUri());
    Assert.assertTrue(ConfigUtils.verifySubset(js3_notified.getConfig(), js3.getConfig()));
    Assert.assertEquals(js3.getVersion(), js3_notified.getVersion());
    ResolvedJobSpec js3_resolved = new ResolvedJobSpec(js3_notified, cat);
    Assert.assertEquals(js3_resolved.getConfig().getString("param1"), "value1");
    Assert.assertEquals(js3_resolved.getConfig().getString("param2"), "value2");
    cat.stopAsync();
    cat.awaitTerminated(10, TimeUnit.SECONDS);
}
Also used : Path(org.apache.hadoop.fs.Path) Hashtable(java.util.Hashtable) Properties(java.util.Properties) URI(java.net.URI) JobCatalogListener(org.apache.gobblin.runtime.api.JobCatalogListener) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ResolvedJobSpec(org.apache.gobblin.runtime.job_spec.ResolvedJobSpec) PathAlterationObserver(org.apache.gobblin.util.filesystem.PathAlterationObserver) ResolvedJobSpec(org.apache.gobblin.runtime.job_spec.ResolvedJobSpec) JobSpec(org.apache.gobblin.runtime.api.JobSpec) PrintWriter(java.io.PrintWriter) Test(org.testng.annotations.Test)

Example 27 with JobSpec

use of org.apache.gobblin.runtime.api.JobSpec in project incubator-gobblin by apache.

the class TestInMemoryJobCatalog method testCallbacks.

@Test
public void testCallbacks() throws Exception {
    InMemoryJobCatalog cat = new InMemoryJobCatalog();
    cat.startAsync();
    cat.awaitRunning(1, TimeUnit.SECONDS);
    JobCatalogListener l = Mockito.mock(JobCatalogListener.class);
    JobSpec js1_1 = JobSpec.builder("test:job1").withVersion("1").build();
    JobSpec js1_2 = JobSpec.builder("test:job1").withVersion("2").build();
    JobSpec js1_3 = JobSpec.builder("test:job1").withVersion("3").build();
    JobSpec js2 = JobSpec.builder("test:job2").withVersion("1").build();
    cat.put(js1_1);
    cat.addListener(l);
    cat.put(js1_2);
    cat.put(js2);
    cat.put(js1_3);
    cat.remove(js2.getUri());
    cat.remove(new URI("test:dummy_job"));
    cat.removeListener(l);
    cat.remove(js1_3.getUri());
    Mockito.verify(l).onAddJob(Mockito.eq(js1_1));
    Mockito.verify(l).onUpdateJob(Mockito.eq(js1_2));
    Mockito.verify(l).onAddJob(Mockito.eq(js2));
    Mockito.verify(l).onUpdateJob(Mockito.eq(js1_3));
    Mockito.verify(l).onDeleteJob(Mockito.eq(js2.getUri()), Mockito.eq(js2.getVersion()));
    Mockito.verifyNoMoreInteractions(l);
    cat.stopAsync();
    cat.awaitTerminated(1, TimeUnit.SECONDS);
}
Also used : JobCatalogListener(org.apache.gobblin.runtime.api.JobCatalogListener) JobSpec(org.apache.gobblin.runtime.api.JobSpec) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 28 with JobSpec

use of org.apache.gobblin.runtime.api.JobSpec in project incubator-gobblin by apache.

the class TestInMemoryJobCatalog method testMetrics.

@SuppressWarnings("unchecked")
@Test
public void testMetrics() throws Exception {
    final Logger log = LoggerFactory.getLogger(getClass().getSimpleName() + ".testMetrics");
    InMemoryJobCatalog cat = new InMemoryJobCatalog(Optional.of(log), Optional.<MetricContext>absent(), true);
    cat.startAsync();
    cat.awaitRunning(1, TimeUnit.SECONDS);
    MetricsAssert ma = new MetricsAssert(cat.getMetricContext());
    JobSpec js1_1 = JobSpec.builder("test:job1").withVersion("1").build();
    JobSpec js1_2 = JobSpec.builder("test:job1").withVersion("2").build();
    JobSpec js1_3 = JobSpec.builder("test:job1").withVersion("3").build();
    JobSpec js2 = JobSpec.builder("test:job2").withVersion("1").build();
    cat.put(js1_1);
    Assert.assertEquals(cat.getMetrics().getNumActiveJobs().getValue().intValue(), 1);
    Assert.assertEquals(cat.getMetrics().getTotalAddCalls().getValue().longValue(), 1);
    Assert.assertEquals(cat.getMetrics().getTotalUpdateCalls().getValue().longValue(), 0);
    Assert.assertEquals(cat.getMetrics().getTotalDeleteCalls().getValue().longValue(), 0);
    ma.assertEvent(Predicates.and(MetricsAssert.eqEventNamespace(JobCatalog.class.getName()), MetricsAssert.eqEventName(JobCatalog.StandardMetrics.TRACKING_EVENT_NAME), MetricsAssert.eqEventMetdata(GobblinMetricsKeys.OPERATION_TYPE_META, JobCatalog.StandardMetrics.JOB_ADDED_OPERATION_TYPE), MetricsAssert.eqEventMetdata(GobblinMetricsKeys.JOB_SPEC_URI_META, js1_1.getUri().toString()), MetricsAssert.eqEventMetdata(GobblinMetricsKeys.JOB_SPEC_VERSION_META, js1_1.getVersion())), 100, TimeUnit.MILLISECONDS);
    cat.put(js1_2);
    Assert.assertEquals(cat.getMetrics().getNumActiveJobs().getValue().intValue(), 1);
    Assert.assertEquals(cat.getMetrics().getTotalAddCalls().getValue().longValue(), 1);
    Assert.assertEquals(cat.getMetrics().getTotalUpdateCalls().getValue().longValue(), 1);
    Assert.assertEquals(cat.getMetrics().getTotalDeleteCalls().getValue().longValue(), 0);
    ma.assertEvent(Predicates.and(MetricsAssert.eqEventNamespace(JobCatalog.class.getName()), MetricsAssert.eqEventName(JobCatalog.StandardMetrics.TRACKING_EVENT_NAME), MetricsAssert.eqEventMetdata(GobblinMetricsKeys.OPERATION_TYPE_META, JobCatalog.StandardMetrics.JOB_UPDATED_OPERATION_TYPE), MetricsAssert.eqEventMetdata(GobblinMetricsKeys.JOB_SPEC_URI_META, js1_2.getUri().toString()), MetricsAssert.eqEventMetdata(GobblinMetricsKeys.JOB_SPEC_VERSION_META, js1_2.getVersion())), 100, TimeUnit.MILLISECONDS);
    cat.put(js2);
    Assert.assertEquals(cat.getMetrics().getNumActiveJobs().getValue().intValue(), 2);
    Assert.assertEquals(cat.getMetrics().getTotalAddCalls().getValue().longValue(), 2);
    Assert.assertEquals(cat.getMetrics().getTotalUpdateCalls().getValue().longValue(), 1);
    Assert.assertEquals(cat.getMetrics().getTotalDeleteCalls().getValue().longValue(), 0);
    ma.assertEvent(Predicates.and(MetricsAssert.eqEventNamespace(JobCatalog.class.getName()), MetricsAssert.eqEventName(JobCatalog.StandardMetrics.TRACKING_EVENT_NAME), MetricsAssert.eqEventMetdata(GobblinMetricsKeys.OPERATION_TYPE_META, JobCatalog.StandardMetrics.JOB_ADDED_OPERATION_TYPE), MetricsAssert.eqEventMetdata(GobblinMetricsKeys.JOB_SPEC_URI_META, js2.getUri().toString()), MetricsAssert.eqEventMetdata(GobblinMetricsKeys.JOB_SPEC_VERSION_META, js2.getVersion())), 100, TimeUnit.MILLISECONDS);
    cat.put(js1_3);
    Assert.assertEquals(cat.getMetrics().getNumActiveJobs().getValue().intValue(), 2);
    Assert.assertEquals(cat.getMetrics().getTotalAddCalls().getValue().longValue(), 2);
    Assert.assertEquals(cat.getMetrics().getTotalUpdateCalls().getValue().longValue(), 2);
    Assert.assertEquals(cat.getMetrics().getTotalDeleteCalls().getValue().longValue(), 0);
    ma.assertEvent(Predicates.and(MetricsAssert.eqEventNamespace(JobCatalog.class.getName()), MetricsAssert.eqEventName(JobCatalog.StandardMetrics.TRACKING_EVENT_NAME), MetricsAssert.eqEventMetdata(GobblinMetricsKeys.OPERATION_TYPE_META, JobCatalog.StandardMetrics.JOB_UPDATED_OPERATION_TYPE), MetricsAssert.eqEventMetdata(GobblinMetricsKeys.JOB_SPEC_URI_META, js1_3.getUri().toString()), MetricsAssert.eqEventMetdata(GobblinMetricsKeys.JOB_SPEC_VERSION_META, js1_3.getVersion())), 100, TimeUnit.MILLISECONDS);
    cat.remove(js2.getUri());
    Assert.assertEquals(cat.getMetrics().getNumActiveJobs().getValue().intValue(), 1);
    Assert.assertEquals(cat.getMetrics().getTotalAddCalls().getValue().longValue(), 2);
    Assert.assertEquals(cat.getMetrics().getTotalUpdateCalls().getValue().longValue(), 2);
    Assert.assertEquals(cat.getMetrics().getTotalDeleteCalls().getValue().longValue(), 1);
    ma.assertEvent(Predicates.and(MetricsAssert.eqEventNamespace(JobCatalog.class.getName()), MetricsAssert.eqEventName(JobCatalog.StandardMetrics.TRACKING_EVENT_NAME), MetricsAssert.eqEventMetdata(GobblinMetricsKeys.OPERATION_TYPE_META, JobCatalog.StandardMetrics.JOB_DELETED_OPERATION_TYPE), MetricsAssert.eqEventMetdata(GobblinMetricsKeys.JOB_SPEC_URI_META, js2.getUri().toString()), MetricsAssert.eqEventMetdata(GobblinMetricsKeys.JOB_SPEC_VERSION_META, js2.getVersion())), 100, TimeUnit.MILLISECONDS);
    cat.remove(new URI("test:dummy_job"));
    Assert.assertEquals(cat.getMetrics().getNumActiveJobs().getValue().intValue(), 1);
    Assert.assertEquals(cat.getMetrics().getTotalAddCalls().getValue().longValue(), 2);
    Assert.assertEquals(cat.getMetrics().getTotalUpdateCalls().getValue().longValue(), 2);
    Assert.assertEquals(cat.getMetrics().getTotalDeleteCalls().getValue().longValue(), 1);
    cat.remove(js1_3.getUri());
    Assert.assertEquals(cat.getMetrics().getNumActiveJobs().getValue().intValue(), 0);
    Assert.assertEquals(cat.getMetrics().getTotalAddCalls().getValue().longValue(), 2);
    Assert.assertEquals(cat.getMetrics().getTotalUpdateCalls().getValue().longValue(), 2);
    Assert.assertEquals(cat.getMetrics().getTotalDeleteCalls().getValue().longValue(), 2);
    ma.assertEvent(Predicates.and(MetricsAssert.eqEventNamespace(JobCatalog.class.getName()), MetricsAssert.eqEventName(JobCatalog.StandardMetrics.TRACKING_EVENT_NAME), MetricsAssert.eqEventMetdata(GobblinMetricsKeys.OPERATION_TYPE_META, JobCatalog.StandardMetrics.JOB_DELETED_OPERATION_TYPE), MetricsAssert.eqEventMetdata(GobblinMetricsKeys.JOB_SPEC_URI_META, js1_3.getUri().toString()), MetricsAssert.eqEventMetdata(GobblinMetricsKeys.JOB_SPEC_VERSION_META, js1_3.getVersion())), 100, TimeUnit.MILLISECONDS);
    cat.stopAsync();
    cat.awaitTerminated(1, TimeUnit.SECONDS);
}
Also used : MetricsAssert(org.apache.gobblin.metrics.test.MetricsAssert) JobSpec(org.apache.gobblin.runtime.api.JobSpec) Logger(org.slf4j.Logger) JobCatalog(org.apache.gobblin.runtime.api.JobCatalog) URI(java.net.URI) Test(org.testng.annotations.Test)

Example 29 with JobSpec

use of org.apache.gobblin.runtime.api.JobSpec in project incubator-gobblin by apache.

the class IdentityFlowToJobSpecCompilerTest method testCompilerWithoutTemplateCatalog.

@Test
public void testCompilerWithoutTemplateCatalog() {
    FlowSpec flowSpec = initFlowSpec();
    // Run compiler on flowSpec
    Map<Spec, SpecExecutor> specExecutorMapping = this.compilerWithoutTemplateCalague.compileFlow(flowSpec);
    // Assert pre-requisites
    Assert.assertNotNull(specExecutorMapping, "Expected non null mapping.");
    Assert.assertTrue(specExecutorMapping.size() == 1, "Exepected 1 executor for FlowSpec.");
    // Assert FlowSpec compilation
    Spec spec = specExecutorMapping.keySet().iterator().next();
    Assert.assertTrue(spec instanceof JobSpec, "Expected JobSpec compiled from FlowSpec.");
    // Assert JobSpec properties
    JobSpec jobSpec = (JobSpec) spec;
    Assert.assertTrue(!jobSpec.getConfig().hasPath("testProperty1"));
    Assert.assertTrue(!jobSpec.getConfig().hasPath("testProperty2"));
    Assert.assertTrue(!jobSpec.getConfig().hasPath("testProperty3"));
    Assert.assertEquals(jobSpec.getConfig().getString(ServiceConfigKeys.FLOW_SOURCE_IDENTIFIER_KEY), TEST_SOURCE_NAME);
    Assert.assertFalse(jobSpec.getConfig().hasPath(ConfigurationKeys.JOB_SCHEDULE_KEY));
    Assert.assertEquals(jobSpec.getConfig().getString(ConfigurationKeys.JOB_NAME_KEY), TEST_FLOW_NAME);
    Assert.assertEquals(jobSpec.getConfig().getString(ConfigurationKeys.JOB_GROUP_KEY), TEST_FLOW_GROUP);
    Assert.assertEquals(jobSpec.getConfig().getString(ConfigurationKeys.FLOW_NAME_KEY), TEST_FLOW_NAME);
    Assert.assertEquals(jobSpec.getConfig().getString(ConfigurationKeys.FLOW_GROUP_KEY), TEST_FLOW_GROUP);
    Assert.assertTrue(jobSpec.getConfig().hasPath(ConfigurationKeys.FLOW_EXECUTION_ID_KEY));
}
Also used : FlowSpec(org.apache.gobblin.runtime.api.FlowSpec) SpecExecutor(org.apache.gobblin.runtime.api.SpecExecutor) InMemorySpecExecutor(org.apache.gobblin.runtime.spec_executorInstance.InMemorySpecExecutor) JobSpec(org.apache.gobblin.runtime.api.JobSpec) TopologySpec(org.apache.gobblin.runtime.api.TopologySpec) JobSpec(org.apache.gobblin.runtime.api.JobSpec) Spec(org.apache.gobblin.runtime.api.Spec) FlowSpec(org.apache.gobblin.runtime.api.FlowSpec) Test(org.testng.annotations.Test)

Example 30 with JobSpec

use of org.apache.gobblin.runtime.api.JobSpec in project incubator-gobblin by apache.

the class MultiHopsFlowToJobSpecCompiler method buildJobSpec.

/**
 * Generate JobSpec based on the #templateURI that user specified.
 */
private JobSpec buildJobSpec(ServiceNode sourceNode, ServiceNode targetNode, URI templateURI, FlowSpec flowSpec) {
    JobSpec jobSpec;
    JobSpec.Builder jobSpecBuilder = JobSpec.builder(jobSpecURIGenerator(flowSpec, sourceNode, targetNode)).withConfig(flowSpec.getConfig()).withDescription(flowSpec.getDescription()).withVersion(flowSpec.getVersion());
    if (templateURI != null) {
        jobSpecBuilder.withTemplate(templateURI);
        try {
            jobSpec = new ResolvedJobSpec(jobSpecBuilder.build(), templateCatalog.get());
            log.info("Resolved JobSpec properties are: " + jobSpec.getConfigAsProperties());
        } catch (SpecNotFoundException | JobTemplate.TemplateException e) {
            throw new RuntimeException("Could not resolve template in JobSpec from TemplateCatalog", e);
        }
    } else {
        jobSpec = jobSpecBuilder.build();
        log.info("Unresolved JobSpec properties are: " + jobSpec.getConfigAsProperties());
    }
    // Remove schedule
    jobSpec.setConfig(jobSpec.getConfig().withoutPath(ConfigurationKeys.JOB_SCHEDULE_KEY));
    // Add job.name and job.group
    if (flowSpec.getConfig().hasPath(ConfigurationKeys.FLOW_NAME_KEY)) {
        jobSpec.setConfig(jobSpec.getConfig().withValue(ConfigurationKeys.JOB_NAME_KEY, ConfigValueFactory.fromAnyRef(flowSpec.getConfig().getValue(ConfigurationKeys.FLOW_NAME_KEY).unwrapped().toString() + "-" + sourceNode.getNodeName() + "-" + targetNode.getNodeName())));
    }
    if (flowSpec.getConfig().hasPath(ConfigurationKeys.FLOW_GROUP_KEY)) {
        jobSpec.setConfig(jobSpec.getConfig().withValue(ConfigurationKeys.JOB_GROUP_KEY, flowSpec.getConfig().getValue(ConfigurationKeys.FLOW_GROUP_KEY)));
    }
    // Add flow execution id for this compilation
    long flowExecutionId = System.currentTimeMillis();
    jobSpec.setConfig(jobSpec.getConfig().withValue(ConfigurationKeys.FLOW_EXECUTION_ID_KEY, ConfigValueFactory.fromAnyRef(flowExecutionId)));
    // Reset properties in Spec from Config
    jobSpec.setConfigAsProperties(ConfigUtils.configToProperties(jobSpec.getConfig()));
    return jobSpec;
}
Also used : SpecNotFoundException(org.apache.gobblin.runtime.api.SpecNotFoundException) ResolvedJobSpec(org.apache.gobblin.runtime.job_spec.ResolvedJobSpec) JobSpec(org.apache.gobblin.runtime.api.JobSpec) ResolvedJobSpec(org.apache.gobblin.runtime.job_spec.ResolvedJobSpec)

Aggregations

JobSpec (org.apache.gobblin.runtime.api.JobSpec)52 Test (org.testng.annotations.Test)34 URI (java.net.URI)18 Properties (java.util.Properties)14 Spec (org.apache.gobblin.runtime.api.Spec)11 SpecExecutor (org.apache.gobblin.runtime.api.SpecExecutor)11 ResolvedJobSpec (org.apache.gobblin.runtime.job_spec.ResolvedJobSpec)9 Map (java.util.Map)8 Pair (org.apache.commons.lang3.tuple.Pair)8 Config (com.typesafe.config.Config)7 Logger (org.slf4j.Logger)7 JobCatalogListener (org.apache.gobblin.runtime.api.JobCatalogListener)6 WriteResponse (org.apache.gobblin.writer.WriteResponse)6 IOException (java.io.IOException)5 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)5 JobExecutionDriver (org.apache.gobblin.runtime.api.JobExecutionDriver)5 JobExecutionResult (org.apache.gobblin.runtime.api.JobExecutionResult)5 JobLifecycleListener (org.apache.gobblin.runtime.api.JobLifecycleListener)5 Path (org.apache.hadoop.fs.Path)5 SpecNotFoundException (org.apache.gobblin.runtime.api.SpecNotFoundException)4