Search in sources :

Example 1 with JobInstance

use of org.apache.samza.rest.proxy.job.JobInstance in project samza by apache.

the class LocalStoreMonitor method getHostAffinityEnabledJobs.

/**
   * Helper method to find and return the list of host affinity enabled jobs on this NM.
   * @param localStoreDirFile the location in which all stores of host affinity enabled jobs are persisted.
   * @return the list of the host affinity enabled jobs that are installed on this NM.
   */
private static List<JobInstance> getHostAffinityEnabledJobs(File localStoreDirFile) {
    List<JobInstance> jobInstances = new ArrayList<>();
    for (File jobStore : localStoreDirFile.listFiles(File::isDirectory)) {
        // Name of the jobStore(jobStorePath) is of the form : ${job.name}-${job.id}.
        String jobStorePath = jobStore.getName();
        int indexSeparator = jobStorePath.lastIndexOf("-");
        if (indexSeparator != -1) {
            jobInstances.add(new JobInstance(jobStorePath.substring(0, indexSeparator), jobStorePath.substring(indexSeparator + 1)));
        }
    }
    return jobInstances;
}
Also used : JobInstance(org.apache.samza.rest.proxy.job.JobInstance) ArrayList(java.util.ArrayList) File(java.io.File)

Example 2 with JobInstance

use of org.apache.samza.rest.proxy.job.JobInstance in project samza by apache.

the class SimpleInstallationFinder method findJobInstances.

/**
   * Finds all the job instances in the specified path and adds a corresponding {@link JobInstance} and
   * {@link InstallationRecord} for each instance.
   *
   * @param jobInstallPath  the path to search for job instances.
   * @param jobs            the map to which the job instances will be added.
   */
private void findJobInstances(final File jobInstallPath, final Map<JobInstance, InstallationRecord> jobs) {
    try {
        String jobInstallCanonPath = jobInstallPath.getCanonicalPath();
        File configPath = Paths.get(jobInstallCanonPath, CFG_SUBPATH).toFile();
        if (!(configPath.exists() && configPath.isDirectory())) {
            log.debug("Config path not found: " + configPath);
            return;
        }
        for (File configFile : configPath.listFiles()) {
            if (configFile.isFile()) {
                String configFilePath = configFile.getCanonicalPath();
                Config config = jobConfigFactory.getConfig(new URI("file://" + configFilePath));
                if (config.containsKey(JobConfig.JOB_NAME()) && config.containsKey(JobConfig.STREAM_JOB_FACTORY_CLASS())) {
                    String jobName = config.get(JobConfig.JOB_NAME());
                    String jobId = config.get(JobConfig.JOB_ID(), "1");
                    JobInstance jobInstance = new JobInstance(jobName, jobId);
                    if (jobs.containsKey(jobInstance)) {
                        throw new IllegalStateException(String.format("Found more than one job config with jobName:%s and jobId:%s", jobName, jobId));
                    }
                    InstallationRecord jobInstall = new InstallationRecord(jobName, jobId, jobInstallCanonPath, configFilePath, getBinPath(jobInstallCanonPath));
                    jobs.put(jobInstance, jobInstall);
                }
            }
        }
    } catch (Exception e) {
        throw new SamzaException("Exception finding job instance in path: " + jobInstallPath, e);
    }
}
Also used : JobInstance(org.apache.samza.rest.proxy.job.JobInstance) JobConfig(org.apache.samza.config.JobConfig) Config(org.apache.samza.config.Config) File(java.io.File) URI(java.net.URI) SamzaException(org.apache.samza.SamzaException) SamzaException(org.apache.samza.SamzaException)

Example 3 with JobInstance

use of org.apache.samza.rest.proxy.job.JobInstance in project samza by apache.

the class MockJobProxy method getAllJobInstances.

@Override
protected Set<JobInstance> getAllJobInstances() {
    Set<JobInstance> validatedInstallations = new LinkedHashSet<>();
    validatedInstallations.add(new JobInstance(JOB_INSTANCE_1_NAME, JOB_INSTANCE_1_ID));
    validatedInstallations.add(new JobInstance(JOB_INSTANCE_2_NAME, JOB_INSTANCE_2_ID));
    validatedInstallations.add(new JobInstance(JOB_INSTANCE_3_NAME, JOB_INSTANCE_3_ID));
    validatedInstallations.add(new JobInstance(JOB_INSTANCE_4_NAME, JOB_INSTANCE_4_ID));
    return validatedInstallations;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) JobInstance(org.apache.samza.rest.proxy.job.JobInstance)

Example 4 with JobInstance

use of org.apache.samza.rest.proxy.job.JobInstance in project samza by apache.

the class JobsResource method updateJobStatus.

/**
   *
   * @param jobName the name of the job as configured in {@link org.apache.samza.config.JobConfig#JOB_NAME}.
   * @param jobId   the id of the job as configured in {@link org.apache.samza.config.JobConfig#JOB_ID}.
   * @param status   the {@link JobStatus} to which the job will transition.
   * @return        a {@link javax.ws.rs.core.Response.Status#ACCEPTED} {@link javax.ws.rs.core.Response}
   *                containing a {@link Job} for the Samza job if it is
   *                installed on this host. {@link javax.ws.rs.core.Response.Status#NOT_FOUND}
   *                {@link javax.ws.rs.core.Response.Status#BAD_REQUEST} and
   *                {@link javax.ws.rs.core.Response.Status#INTERNAL_SERVER_ERROR} can occur for corresponding errors.
   */
@PUT
@Path("/{jobName}/{jobId}")
@Produces(MediaType.APPLICATION_JSON)
public Response updateJobStatus(@PathParam("jobName") final String jobName, @PathParam("jobId") final String jobId, @QueryParam("status") String status) {
    JobInstance jobInstance = new JobInstance(jobName, jobId);
    try {
        if (!jobProxy.jobExists(jobInstance)) {
            return Response.status(Response.Status.NOT_FOUND).entity(Collections.singletonMap("message", String.format("Job %s instance %s is not installed on this host.", jobName, jobId))).build();
        }
        if (status == null) {
            throw new IllegalArgumentException("Unrecognized status parameter: " + status);
        }
        JobStatus samzaStatus = JobStatus.valueOf(status.toUpperCase());
        switch(samzaStatus) {
            case STARTED:
                log.info("Starting {}", jobInstance);
                jobProxy.start(jobInstance);
                Job infoStarted = jobProxy.getJobStatus(jobInstance);
                return Response.accepted(infoStarted).build();
            case STOPPED:
                log.info("Stopping {}", jobInstance);
                jobProxy.stop(jobInstance);
                Job infoStopped = jobProxy.getJobStatus(jobInstance);
                return Response.accepted(infoStopped).build();
            default:
                throw new IllegalArgumentException("Unsupported status: " + status);
        }
    } catch (IllegalArgumentException e) {
        log.info(String.format("Illegal arguments updateJobStatus. JobName:%s JobId:%s Status=%s", jobName, jobId, status), e);
        return Responses.badRequestResponse(e.getMessage());
    } catch (Exception e) {
        log.error("Error in updateJobStatus.", e);
        return Responses.errorResponse(String.format("Error type: %s message: %s", e.toString(), e.getMessage()));
    }
}
Also used : JobStatus(org.apache.samza.rest.model.JobStatus) JobInstance(org.apache.samza.rest.proxy.job.JobInstance) Job(org.apache.samza.rest.model.Job) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 5 with JobInstance

use of org.apache.samza.rest.proxy.job.JobInstance in project samza by apache.

the class TestLocalStoreMonitor method shouldContinueLocalStoreCleanUpAfterFailureToCleanUpStoreOfAJob.

@Test
public void shouldContinueLocalStoreCleanUpAfterFailureToCleanUpStoreOfAJob() throws Exception {
    File testFailingJobDir = new File(System.getProperty("java.io.tmpdir") + File.separator + "samza-test-job/", "test-jobName-jobId-1");
    File testFailingTaskStoreDir = new File(new File(testFailingJobDir, "test-store"), "test-task");
    FileUtils.forceMkdir(testFailingTaskStoreDir);
    // For job: test-jobName-jobId-1, throw up in getTasks call and
    // expect the cleanup to succeed for other job: test-jobName-jobId.
    Mockito.doThrow(new RuntimeException("Dummy exception message.")).when(jobsClientMock).getTasks(new JobInstance("test-jobName", "jobId-1"));
    Task task = new Task("notLocalHost", "test-task", "0", new ArrayList<>(), ImmutableList.of("test-store"));
    Mockito.when(jobsClientMock.getTasks(new JobInstance("test-jobName", "jobId"))).thenReturn(ImmutableList.of(task));
    Map<String, String> configMap = new HashMap<>(config);
    configMap.put(LocalStoreMonitorConfig.CONFIG_IGNORE_FAILURES, "true");
    LocalStoreMonitor localStoreMonitor = new LocalStoreMonitor(new LocalStoreMonitorConfig(new MapConfig(configMap)), localStoreMonitorMetrics, jobsClientMock);
    localStoreMonitor.monitor();
    // Non failing job directory should be cleaned up.
    assertTrue("Task store directory should not exist.", !taskStoreDir.exists());
    FileUtils.deleteDirectory(testFailingJobDir);
}
Also used : Task(org.apache.samza.rest.model.Task) JobInstance(org.apache.samza.rest.proxy.job.JobInstance) HashMap(java.util.HashMap) MapConfig(org.apache.samza.config.MapConfig) File(java.io.File) Test(org.junit.Test)

Aggregations

JobInstance (org.apache.samza.rest.proxy.job.JobInstance)6 File (java.io.File)4 JobStatus (org.apache.samza.rest.model.JobStatus)2 Task (org.apache.samza.rest.model.Task)2 IOException (java.io.IOException)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 PUT (javax.ws.rs.PUT)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 SamzaException (org.apache.samza.SamzaException)1 Config (org.apache.samza.config.Config)1 JobConfig (org.apache.samza.config.JobConfig)1 MapConfig (org.apache.samza.config.MapConfig)1 TaskName (org.apache.samza.container.TaskName)1 Job (org.apache.samza.rest.model.Job)1 Test (org.junit.Test)1