Skip to content

Commit

Permalink
Merge pull request #816 from jsanda/hwkmetrics-648
Browse files Browse the repository at this point in the history
[HWKMETRICS-648] add support for modifying and persisting job params
  • Loading branch information
stefannegrea committed Apr 21, 2017
2 parents 149f49d + 9e5d47d commit 4f35ab7
Show file tree
Hide file tree
Showing 15 changed files with 577 additions and 171 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -48,7 +48,7 @@ install:
# unshallow is needed by license-maven-plugin
- git fetch origin --unshallow
script:
- ./mvnw verify -Dtest.logging.console.level=INFO -Dwildfly.logging.console.level=DEBUG -Djboss-as.logging.console.level=INFO -Dterminal-event.timeout=25 -Dtravis -B
- ./mvnw verify -Dtest.logging.console.level=INFO -Dwildfly.logging.console.level=INFO -Djboss-as.logging.console.level=INFO -Dterminal-event.timeout=25 -Dtravis -B
before_cache:
# Clean the cached directories once their size exceeds the space left on the partition.
# This is important because Travis zips the cached directories to a temporary file on the same partition.
Expand Down
Expand Up @@ -31,7 +31,7 @@ public interface JobsService {

void shutdown();

Single<JobDetails> submitDeleteTenantJob(String tenantId, String jobName);
Single<? extends JobDetails> submitDeleteTenantJob(String tenantId, String jobName);

Single<JobDetails> submitDeleteExpiredMetricsJob(long expiration, String jobName);
Single<? extends JobDetails> submitDeleteExpiredMetricsJob(long expiration, String jobName);
}
Expand Up @@ -131,13 +131,13 @@ public void shutdown() {
}

@Override
public Single<JobDetails> submitDeleteTenantJob(String tenantId, String jobName) {
public Single<? extends JobDetails> submitDeleteTenantJob(String tenantId, String jobName) {
return scheduler.scheduleJob(DeleteTenant.JOB_NAME, jobName, ImmutableMap.of("tenantId", tenantId),
new SingleExecutionTrigger.Builder().withDelay(1, TimeUnit.MINUTES).build());
}

@Override
public Single<JobDetails> submitDeleteExpiredMetricsJob(long expiration, String jobName) {
public Single<? extends JobDetails> submitDeleteExpiredMetricsJob(long expiration, String jobName) {
return scheduler.scheduleJob(DeleteExpiredMetrics.JOB_NAME, jobName,
ImmutableMap.of("expirationTimestamp", expiration + ""),
new SingleExecutionTrigger.Builder().withDelay(1, TimeUnit.MINUTES).build());
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2016 Red Hat, Inc. and/or its affiliates
* Copyright 2014-2017 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -16,99 +16,44 @@
*/
package org.hawkular.metrics.scheduler.api;

import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

import com.google.common.base.MoreObjects;

/**
* Provides information about scheduled jobs.
*
* @author jsanda
*/
public class JobDetails {

private UUID jobId;

private String jobType;

private String jobName;

private Map<String, String> parameters;

private Trigger trigger;

private JobStatus status;

public JobDetails(UUID jobId, String jobType, String jobName, Map<String, String> parameters, Trigger trigger) {
this.jobId = jobId;
this.jobType = jobType;
this.jobName = jobName;
this.parameters = Collections.unmodifiableMap(parameters);
this.trigger = trigger;
status = JobStatus.NONE;
}

public JobDetails(UUID jobId, String jobType, String jobName, Map<String, String> parameters, Trigger trigger,
JobStatus status) {
this.jobId = jobId;
this.jobType = jobType;
this.jobName = jobName;
this.parameters = Collections.unmodifiableMap(parameters);
this.trigger = trigger;
this.status = status;
}

public UUID getJobId() {
return jobId;
}

public String getJobType() {
return jobType;
}

public String getJobName() {
return jobName;
}

public Map<String, String> getParameters() {
return parameters;
}

public Trigger getTrigger() {
return trigger;
}

public JobStatus getStatus() {
return status;
}

@Override public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
JobDetails details = (JobDetails) o;
return Objects.equals(jobId, details.jobId) &&
Objects.equals(jobType, details.jobType) &&
Objects.equals(jobName, details.jobName) &&
Objects.equals(parameters, details.parameters) &&
Objects.equals(trigger, details.trigger) &&
Objects.equals(status, details.status);
}

@Override public int hashCode() {
return Objects.hash(jobId, jobType, jobName, parameters, trigger, status);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("jobId", jobId)
.add("jobType", jobType)
.add("jobName", jobName)
.add("parameters", parameters)
.add("trigger", trigger)
.add("status", status)
.omitNullValues()
.toString();
}
public interface JobDetails {

/**
* A unique identifier that the scheduler uses to query Cassandra for the job details
*/
UUID getJobId();

/**
* Every job has a type. The scheduler uses the type to determine who is responsible for the job execution.
* @see Scheduler#register(String, rx.functions.Func1)
* @see Scheduler#register(String, rx.functions.Func1, rx.functions.Func2)
*/
String getJobType();

/**
* Note that thee job name does not have to be unique.
*/
String getJobName();

/**
* The job {@link JobParameters parameters} which are mutable
*/
JobParameters getParameters();

/**
* The {@link Trigger trigger} specifies when the job will execute.
*/
Trigger getTrigger();

/**
* This is primarily for internal use by the scheduler.
*/
JobStatus getStatus();
}
@@ -0,0 +1,67 @@
/*
* Copyright 2014-2017 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.metrics.scheduler.api;

import java.util.Map;

import rx.Completable;

/**
* Provides a map-like key/value API for job parameters. The parameters are mutable. Changes will be persisted after
* job execution completes (for repeating jobs). Changes can also be persisted during execution using the
* {@link #save() save} method.
*
* @author jsanda
*/
public interface JobParameters {

/**
* Return the value associated with the key or null if there is no such parameter
*/
String get(String key);

/**
* Associates the value with the key and returns the old value if there previously was a mapping for the key
*/
String put(String key, String value);

/**
* Removes the value associated with the key or null if there is no such parameter
*/
String remove(String key);

/**
* Return true if the parameters contain a value for the key
*/
boolean containsKey(String key);

/**
* Return an immutable map of the parameters. Note that this map is a copy. Any changes made to the parameters
* through methods like {@link #get(String) get} or {@link #put(String, String) put} will not be reflected in
* this map.
*/
Map<String, String> getMap();

/**
* Asynchronously save the parameters back to Cassandra. For reoccurring jobs any changes to parameters will
* automatically be persisted when the job finishes and is rescheduled for its next run. This method can be useful
* for long running jobs that perform a lot of work. It can be used to create checkpoints so that if a job is
* abruptly stopped and restarted it can resume its work from that checkpoint rather than starting all over.
*/
Completable save();

}
Expand Up @@ -39,7 +39,7 @@ public interface Scheduler {
* @param trigger
* @return A Single that emits the job details
*/
Single<JobDetails> scheduleJob(String type, String name, Map<String, String> parameters, Trigger trigger);
Single<? extends JobDetails> scheduleJob(String type, String name, Map<String, String> parameters, Trigger trigger);

/**
* Deletes all the scheduled execution for a job id.
Expand Down
@@ -0,0 +1,142 @@
/*
* Copyright 2014-2017 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hawkular.metrics.scheduler.impl;

import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.function.Function;

import org.hawkular.metrics.scheduler.api.JobDetails;
import org.hawkular.metrics.scheduler.api.JobParameters;
import org.hawkular.metrics.scheduler.api.JobStatus;
import org.hawkular.metrics.scheduler.api.Trigger;

import com.google.common.base.MoreObjects;

import rx.Completable;

/**
* @author jsanda
*/
public class JobDetailsImpl implements JobDetails {

private UUID jobId;

private String jobType;

private String jobName;

private JobParametersImpl parameters;

private Trigger trigger;

private JobStatus status;

public JobDetailsImpl(UUID jobId, String jobType, String jobName, JobParametersImpl parameters, Trigger trigger) {
this.jobId = jobId;
this.jobType = jobType;
this.jobName = jobName;
this.parameters = parameters;
this.trigger = trigger;
status = JobStatus.NONE;
}

public JobDetailsImpl(UUID jobId, String jobType, String jobName, JobParametersImpl parameters, Trigger trigger,
JobStatus status) {
this.jobId = jobId;
this.jobType = jobType;
this.jobName = jobName;
this.parameters = parameters;
this.trigger = trigger;
this.status = status;
}

public JobDetailsImpl(JobDetailsImpl details, Trigger newTrigger) {
jobId = details.jobId;
jobType = details.jobType;
jobName = details.jobName;
parameters = details.parameters;
status = details.status;
trigger = newTrigger;
}

@Override
public UUID getJobId() {
return jobId;
}

@Override
public String getJobType() {
return jobType;
}

@Override
public String getJobName() {
return jobName;
}

@Override
public JobParameters getParameters() {
return parameters;
}

public void setSaveParameters(Function<Map<String, String>, Completable> saveParameters) {
parameters.setSaveParameters(saveParameters);
}

@Override
public Trigger getTrigger() {
return trigger;
}

@Override
public JobStatus getStatus() {
return status;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
JobDetailsImpl details = (JobDetailsImpl) o;
return Objects.equals(jobId, details.jobId) &&
Objects.equals(jobType, details.jobType) &&
Objects.equals(jobName, details.jobName) &&
Objects.equals(parameters, details.parameters) &&
Objects.equals(trigger, details.trigger) &&
Objects.equals(status, details.status);
}

@Override
public int hashCode() {
return Objects.hash(jobId, jobType, jobName, parameters, trigger, status);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("jobId", jobId)
.add("jobType", jobType)
.add("jobName", jobName)
.add("parameters", parameters)
.add("trigger", trigger)
.add("status", status)
.omitNullValues()
.toString();
}
}

0 comments on commit 4f35ab7

Please sign in to comment.