Search in sources :

Example 1 with ResourceConfig

use of com.hazelcast.jet.config.ResourceConfig in project hazelcast-jet by hazelcast.

the class JobRepository method uploadJobResources.

/**
 * Uploads job resources and returns a unique job id generated for the job.
 * If the upload process fails for any reason, such as being unable to access a resource,
 * uploaded resources are cleaned up.
 */
public long uploadJobResources(JobConfig jobConfig) {
    long jobId = newJobId();
    IMap<String, Object> jobResourcesMap = getJobResources(jobId);
    for (ResourceConfig rc : jobConfig.getResourceConfigs()) {
        Map<String, byte[]> tmpMap = new HashMap<>();
        if (rc.isArchive()) {
            try {
                loadJar(tmpMap, rc.getUrl());
            } catch (IOException e) {
                cleanupJobResourcesAndSnapshots(jobId, jobResourcesMap);
                randomIds.remove(jobId);
                throw new JetException("Job resource upload failed", e);
            }
        } else {
            try {
                InputStream in = rc.getUrl().openStream();
                readStreamAndPutCompressedToMap(rc.getId(), tmpMap, in);
            } catch (IOException e) {
                cleanupJobResourcesAndSnapshots(jobId, jobResourcesMap);
                randomIds.remove(jobId);
                throw new JetException("Job resource upload failed", e);
            }
        }
        // now upload it all
        jobResourcesMap.putAll(tmpMap);
    }
    // the marker object will be used to decide when to clean up job resources
    jobResourcesMap.put(RESOURCE_MARKER, System.currentTimeMillis());
    return jobId;
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BufferedInputStream(java.io.BufferedInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) Util.idToString(com.hazelcast.jet.impl.util.Util.idToString) ResourceConfig(com.hazelcast.jet.config.ResourceConfig) IOException(java.io.IOException) JetException(com.hazelcast.jet.JetException)

Example 2 with ResourceConfig

use of com.hazelcast.jet.config.ResourceConfig in project hazelcast-jet by hazelcast.

the class ResourceConfigTest method testAddJar_with_Path.

@Test
public void testAddJar_with_Path() throws Exception {
    JobConfig config = new JobConfig();
    String path = "/path/to/jarfile";
    config.addJar(path);
    ResourceConfig resourceConfig = config.getResourceConfigs().iterator().next();
    assertNull(resourceConfig.getId());
    assertTrue(resourceConfig.isArchive());
    assertEquals(new File(path).toURI().toURL(), resourceConfig.getUrl());
}
Also used : ResourceConfig(com.hazelcast.jet.config.ResourceConfig) File(java.io.File) JobConfig(com.hazelcast.jet.config.JobConfig) Test(org.junit.Test)

Example 3 with ResourceConfig

use of com.hazelcast.jet.config.ResourceConfig in project hazelcast-jet by hazelcast.

the class ResourceConfigTest method testAddResource_with_Path_and_ResourceName.

@Test
public void testAddResource_with_Path_and_ResourceName() throws Exception {
    JobConfig config = new JobConfig();
    String resourceName = "resourceFileName";
    String path = "/path/to/jarfile";
    config.addResource(path, resourceName);
    ResourceConfig resourceConfig = config.getResourceConfigs().iterator().next();
    assertEquals(resourceName, resourceConfig.getId());
    assertFalse(resourceConfig.isArchive());
    assertEquals(new File(path).toURI().toURL(), resourceConfig.getUrl());
}
Also used : ResourceConfig(com.hazelcast.jet.config.ResourceConfig) File(java.io.File) JobConfig(com.hazelcast.jet.config.JobConfig) Test(org.junit.Test)

Example 4 with ResourceConfig

use of com.hazelcast.jet.config.ResourceConfig in project hazelcast-jet by hazelcast.

the class ResourceConfigTest method testAddResource_with_File_and_ResourceName.

@Test
public void testAddResource_with_File_and_ResourceName() throws Exception {
    JobConfig config = new JobConfig();
    String resourceName = "resourceFileName";
    File file = new File("/path/to/resource");
    config.addResource(file, resourceName);
    ResourceConfig resourceConfig = config.getResourceConfigs().iterator().next();
    assertEquals(resourceName, resourceConfig.getId());
    assertFalse(resourceConfig.isArchive());
    assertEquals(file.toURI().toURL(), resourceConfig.getUrl());
}
Also used : ResourceConfig(com.hazelcast.jet.config.ResourceConfig) File(java.io.File) JobConfig(com.hazelcast.jet.config.JobConfig) Test(org.junit.Test)

Example 5 with ResourceConfig

use of com.hazelcast.jet.config.ResourceConfig in project hazelcast-jet by hazelcast.

the class ResourceConfigTest method testAddJar_with_Url.

@Test
public void testAddJar_with_Url() throws Exception {
    JobConfig config = new JobConfig();
    String urlString = "file://path/to/jarfile";
    config.addJar(new URL(urlString));
    ResourceConfig resourceConfig = config.getResourceConfigs().iterator().next();
    assertNull(resourceConfig.getId());
    assertTrue(resourceConfig.isArchive());
    assertEquals(urlString, resourceConfig.getUrl().toString());
}
Also used : ResourceConfig(com.hazelcast.jet.config.ResourceConfig) JobConfig(com.hazelcast.jet.config.JobConfig) URL(java.net.URL) Test(org.junit.Test)

Aggregations

ResourceConfig (com.hazelcast.jet.config.ResourceConfig)11 JobConfig (com.hazelcast.jet.config.JobConfig)10 Test (org.junit.Test)10 File (java.io.File)6 URL (java.net.URL)3 JetException (com.hazelcast.jet.JetException)1 Util.idToString (com.hazelcast.jet.impl.util.Util.idToString)1 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 JarInputStream (java.util.jar.JarInputStream)1