Search in sources :

Example 1 with PathAlterationObserver

use of org.apache.gobblin.util.filesystem.PathAlterationObserver 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 2 with PathAlterationObserver

use of org.apache.gobblin.util.filesystem.PathAlterationObserver in project incubator-gobblin by apache.

the class SchedulerUtils method addPathAlterationObserver.

/**
 * Add {@link PathAlterationObserverScheduler}s for the given
 * root directory and any nested subdirectories under the root directory to the given
 * {@link PathAlterationObserverScheduler}.
 *
 * @param monitor a {@link PathAlterationObserverScheduler}
 * @param listener a {@link org.apache.gobblin.util.filesystem.PathAlterationListener}
 * @param rootDirPath root directory
 */
public static void addPathAlterationObserver(PathAlterationObserverScheduler monitor, PathAlterationListener listener, Path rootDirPath) throws IOException {
    PathAlterationObserver observer = new PathAlterationObserver(rootDirPath);
    observer.addListener(listener);
    monitor.addObserver(observer);
}
Also used : PathAlterationObserver(org.apache.gobblin.util.filesystem.PathAlterationObserver)

Aggregations

PathAlterationObserver (org.apache.gobblin.util.filesystem.PathAlterationObserver)2 PrintWriter (java.io.PrintWriter)1 URI (java.net.URI)1 Hashtable (java.util.Hashtable)1 Properties (java.util.Properties)1 JobCatalogListener (org.apache.gobblin.runtime.api.JobCatalogListener)1 JobSpec (org.apache.gobblin.runtime.api.JobSpec)1 ResolvedJobSpec (org.apache.gobblin.runtime.job_spec.ResolvedJobSpec)1 Path (org.apache.hadoop.fs.Path)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Test (org.testng.annotations.Test)1