use of io.cdap.cdap.proto.Notification in project cdap by cdapio.
the class ScheduleTaskPublisher method publishNotification.
/**
* Publish notification for the triggered schedule
* @param notificationType type of the notification
* @param scheduleId {@link ScheduleId} of the triggered schedule
* @param systemOverrides Arguments that would be supplied as system runtime arguments for the program.
* @param userOverrides Arguments to add to the user runtime arguments for the program.
*/
public void publishNotification(Notification.Type notificationType, ScheduleId scheduleId, Map<String, String> systemOverrides, Map<String, String> userOverrides) throws Exception {
Map<String, String> properties = new HashMap<>();
properties.put(ProgramOptionConstants.SCHEDULE_ID, GSON.toJson(scheduleId));
properties.put(ProgramOptionConstants.SYSTEM_OVERRIDES, GSON.toJson(systemOverrides));
properties.put(ProgramOptionConstants.USER_OVERRIDES, GSON.toJson(userOverrides));
Notification notification = new Notification(notificationType, properties);
messagingService.publish(StoreRequestBuilder.of(topicId).addPayload(GSON.toJson(notification)).build());
}
use of io.cdap.cdap.proto.Notification in project cdap by cdapio.
the class JobQueueTableTest method testJobTimeout.
@Test
public void testJobTimeout() {
TransactionRunners.run(transactionRunner, context -> {
JobQueueTable jobQueue = JobQueueTable.getJobQueue(context, getCConf());
// should be 0 jobs in the JobQueue to begin with
Assert.assertEquals(0, getAllJobs(jobQueue, false).size());
// Construct a partition notification with DATASET_ID
Notification notification = Notification.forPartitions(DATASET_ID, ImmutableList.of());
Assert.assertNull(jobQueue.getJob(SCHED1_JOB.getJobKey()));
ProgramSchedule scheduleWithTimeout = new ProgramSchedule("SCHED1", "one partition schedule", WORKFLOW_ID, ImmutableMap.of("prop3", "abc"), new PartitionTrigger(DATASET_ID, 1), ImmutableList.of());
Job jobWithTimeout = new SimpleJob(scheduleWithTimeout, 0, System.currentTimeMillis() - (Schedulers.JOB_QUEUE_TIMEOUT_MILLIS + 1), Lists.newArrayList(), Job.State.PENDING_TRIGGER, 0L);
jobQueue.put(jobWithTimeout);
Assert.assertEquals(jobWithTimeout, jobQueue.getJob(jobWithTimeout.getJobKey()));
// before adding the notification, there should just be the job we added
Assert.assertEquals(1, toSet(jobQueue.getJobsForSchedule(scheduleWithTimeout.getScheduleId()), true).size());
// adding a notification will ignore the existing job (because it is timed out). It will create a new job
// and add the notification to that new job
jobQueue.addNotification(new ProgramScheduleRecord(SCHED1, new ProgramScheduleMeta(ProgramScheduleStatus.SCHEDULED, 0L)), notification);
List<Job> jobs = new ArrayList<>(toSet(jobQueue.getJobsForSchedule(scheduleWithTimeout.getScheduleId()), true));
// sort by creation time (oldest will be first in the list)
Collections.sort(jobs, (o1, o2) -> Long.valueOf(o1.getCreationTime()).compareTo(o2.getCreationTime()));
Assert.assertEquals(2, jobs.size());
Job firstJob = jobs.get(0);
// first job should have the same creation timestamp as the initially added job
Assert.assertEquals(jobWithTimeout.getCreationTime(), firstJob.getCreationTime());
// the notification we added shouldn't be in the first job
Assert.assertEquals(0, firstJob.getNotifications().size());
// first job should be marked to be deleted because it timed out
Assert.assertTrue(firstJob.isToBeDeleted());
// first job should have the same generation id as the timed out job
Assert.assertEquals(jobWithTimeout.getGenerationId(), firstJob.getGenerationId());
Job secondJob = jobs.get(1);
// first job should not have the same creation timestamp as the initially added job
Assert.assertNotEquals(jobWithTimeout.getCreationTime(), secondJob.getCreationTime());
// the notification we added shouldn't be in the first job
Assert.assertEquals(1, secondJob.getNotifications().size());
Assert.assertEquals(notification, secondJob.getNotifications().get(0));
// first job should not be marked to be deleted, since it was just created by our call to
// JobQueue#addNotification
Assert.assertFalse(secondJob.isToBeDeleted());
// second job should have the next generation id
Assert.assertEquals(jobWithTimeout.getGenerationId() + 1, secondJob.getGenerationId());
});
}
use of io.cdap.cdap.proto.Notification in project cdap by cdapio.
the class JobQueueTableTest method testAddNotifications.
@Test
public void testAddNotifications() {
TransactionRunners.run(transactionRunner, context -> {
JobQueueTable jobQueue = JobQueueTable.getJobQueue(context, getCConf());
// should be 0 jobs in the JobQueue to begin with
Assert.assertEquals(0, getAllJobs(jobQueue, false).size());
// Construct a partition notification with DATASET_ID
Notification notification = Notification.forPartitions(DATASET_ID, ImmutableList.of());
Assert.assertNull(jobQueue.getJob(SCHED1_JOB.getJobKey()));
jobQueue.put(SCHED1_JOB);
Assert.assertEquals(SCHED1_JOB, jobQueue.getJob(SCHED1_JOB.getJobKey()));
// Since notification and SCHED1 have the same dataset id DATASET_ID, notification will be added to
// SCHED1_JOB, which is a job in SCHED1
jobQueue.addNotification(new ProgramScheduleRecord(SCHED1, new ProgramScheduleMeta(ProgramScheduleStatus.SCHEDULED, 0L)), notification);
Assert.assertEquals(ImmutableList.of(notification), jobQueue.getJob(SCHED1_JOB.getJobKey()).getNotifications());
});
}
use of io.cdap.cdap.proto.Notification in project cdap by cdapio.
the class JobQueueTableTest method addNotificationToSchedule.
private void addNotificationToSchedule(ProgramSchedule programSchedule) {
TransactionRunners.run(transactionRunner, context -> {
JobQueueTable jobQueue = JobQueueTable.getJobQueue(context, getCConf());
// Construct a partition notification with DATASET_ID
Notification notification = Notification.forPartitions(DATASET_ID, ImmutableList.of());
jobQueue.addNotification(new ProgramScheduleRecord(programSchedule, new ProgramScheduleMeta(ProgramScheduleStatus.SCHEDULED, 0L)), notification);
});
}
use of io.cdap.cdap.proto.Notification in project cdap by cdapio.
the class RuntimeServiceMainTest method createProgramStateWriter.
/**
* Creates a {@link ProgramStateWriter} that writes to {@link RuntimeClient} directly.
*
* @param injector the injector for creating the {@link RuntimeClient}
* @param programRunId the {@link ProgramRunId} for the program state change
* @return a {@link ProgramStateWriter}
*/
private ProgramStateWriter createProgramStateWriter(Injector injector, ProgramRunId programRunId) {
RuntimeClient runtimeClient = injector.getInstance(RuntimeClient.class);
// We write to the record event directly to skip the app-fabric to process it
// This is because we don't follow the normal event flow here for testing
TopicId topicId = NamespaceId.SYSTEM.topic(injector.getInstance(CConfiguration.class).get(Constants.AppFabric.PROGRAM_STATUS_RECORD_EVENT_TOPIC));
RetryStrategy retryStrategy = RetryStrategies.timeLimit(5, TimeUnit.SECONDS, RetryStrategies.fixDelay(200, TimeUnit.MILLISECONDS));
return new MessagingProgramStateWriter((notificationType, properties) -> {
Notification notification = new Notification(notificationType, properties);
try {
Retries.callWithRetries((Retries.Callable<Void, Exception>) () -> {
runtimeClient.sendMessages(programRunId, topicId, Collections.singleton(createMessage(notification)).iterator());
return null;
}, retryStrategy, t -> t instanceof IOException || t instanceof RetryableException);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
Aggregations