use of co.cask.cdap.common.AlreadyExistsException in project cdap by caskdata.
the class ScheduleClient method doAdd.
/*------------ private helpers ---------------------*/
private void doAdd(ScheduleId scheduleId, String json) throws IOException, UnauthenticatedException, NotFoundException, UnauthorizedException, AlreadyExistsException {
String path = String.format("apps/%s/versions/%s/schedules/%s", scheduleId.getApplication(), scheduleId.getVersion(), scheduleId.getSchedule());
URL url = config.resolveNamespacedURLV3(scheduleId.getNamespaceId(), path);
HttpRequest request = HttpRequest.put(url).withBody(json).build();
HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_CONFLICT);
if (HttpURLConnection.HTTP_NOT_FOUND == response.getResponseCode()) {
throw new NotFoundException(scheduleId);
} else if (HttpURLConnection.HTTP_CONFLICT == response.getResponseCode()) {
throw new AlreadyExistsException(scheduleId);
}
}
use of co.cask.cdap.common.AlreadyExistsException in project cdap by caskdata.
the class CoreSchedulerServiceTest method addListDeleteSchedules.
@Test
public void addListDeleteSchedules() throws Exception {
// verify that list returns nothing
Assert.assertTrue(scheduler.listSchedules(APP1_ID).isEmpty());
Assert.assertTrue(scheduler.listSchedules(PROG1_ID).isEmpty());
// add a schedule for app1
ProgramSchedule tsched1 = new ProgramSchedule("tsched1", "one time schedule", PROG1_ID, ImmutableMap.of("prop1", "nn"), new TimeTrigger("* * ? * 1"), ImmutableList.<Constraint>of());
scheduler.addSchedule(tsched1);
Assert.assertEquals(tsched1, scheduler.getSchedule(TSCHED1_ID));
Assert.assertEquals(ImmutableList.of(tsched1), scheduler.listSchedules(APP1_ID));
Assert.assertEquals(ImmutableList.of(tsched1), scheduler.listSchedules(PROG1_ID));
// add three more schedules, one for the same program, one for the same app, one for another app
ProgramSchedule psched1 = new ProgramSchedule("psched1", "one partition schedule", PROG1_ID, ImmutableMap.of("prop3", "abc"), new PartitionTrigger(DS1_ID, 1), ImmutableList.<Constraint>of());
ProgramSchedule tsched11 = new ProgramSchedule("tsched11", "two times schedule", PROG11_ID, ImmutableMap.of("prop2", "xx"), new TimeTrigger("* * ? * 1,2"), ImmutableList.<Constraint>of());
ProgramSchedule psched2 = new ProgramSchedule("psched2", "two partition schedule", PROG2_ID, ImmutableMap.of("propper", "popper"), new PartitionTrigger(DS2_ID, 2), ImmutableList.<Constraint>of());
scheduler.addSchedules(ImmutableList.of(psched1, tsched11, psched2));
Assert.assertEquals(psched1, scheduler.getSchedule(PSCHED1_ID));
Assert.assertEquals(tsched11, scheduler.getSchedule(TSCHED11_ID));
Assert.assertEquals(psched2, scheduler.getSchedule(PSCHED2_ID));
// list by app and program
Assert.assertEquals(ImmutableList.of(psched1, tsched1), scheduler.listSchedules(PROG1_ID));
Assert.assertEquals(ImmutableList.of(tsched11), scheduler.listSchedules(PROG11_ID));
Assert.assertEquals(ImmutableList.of(psched2), scheduler.listSchedules(PROG2_ID));
Assert.assertEquals(ImmutableList.of(psched1, tsched1, tsched11), scheduler.listSchedules(APP1_ID));
Assert.assertEquals(ImmutableList.of(psched2), scheduler.listSchedules(APP2_ID));
// delete one schedule
scheduler.deleteSchedule(TSCHED1_ID);
verifyNotFound(scheduler, TSCHED1_ID);
Assert.assertEquals(ImmutableList.of(psched1), scheduler.listSchedules(PROG1_ID));
Assert.assertEquals(ImmutableList.of(tsched11), scheduler.listSchedules(PROG11_ID));
Assert.assertEquals(ImmutableList.of(psched2), scheduler.listSchedules(PROG2_ID));
Assert.assertEquals(ImmutableList.of(psched1, tsched11), scheduler.listSchedules(APP1_ID));
Assert.assertEquals(ImmutableList.of(psched2), scheduler.listSchedules(APP2_ID));
// attempt to delete it again along with another one that exists
try {
scheduler.deleteSchedules(ImmutableList.of(TSCHED1_ID, TSCHED11_ID));
Assert.fail("expected NotFoundException");
} catch (NotFoundException e) {
// expected
}
Assert.assertEquals(ImmutableList.of(psched1), scheduler.listSchedules(PROG1_ID));
Assert.assertEquals(ImmutableList.of(tsched11), scheduler.listSchedules(PROG11_ID));
Assert.assertEquals(ImmutableList.of(psched2), scheduler.listSchedules(PROG2_ID));
Assert.assertEquals(ImmutableList.of(psched1, tsched11), scheduler.listSchedules(APP1_ID));
Assert.assertEquals(ImmutableList.of(psched2), scheduler.listSchedules(APP2_ID));
// attempt to add it back together with a schedule that exists
try {
scheduler.addSchedules(ImmutableList.of(tsched1, tsched11));
Assert.fail("expected AlreadyExistsException");
} catch (AlreadyExistsException e) {
// expected
}
Assert.assertEquals(ImmutableList.of(psched1), scheduler.listSchedules(PROG1_ID));
Assert.assertEquals(ImmutableList.of(tsched11), scheduler.listSchedules(PROG11_ID));
Assert.assertEquals(ImmutableList.of(psched2), scheduler.listSchedules(PROG2_ID));
Assert.assertEquals(ImmutableList.of(psched1, tsched11), scheduler.listSchedules(APP1_ID));
Assert.assertEquals(ImmutableList.of(psched2), scheduler.listSchedules(APP2_ID));
// add it back, delete all schedules for one app
scheduler.addSchedule(tsched1);
scheduler.deleteSchedules(APP1_ID);
verifyNotFound(scheduler, TSCHED1_ID);
verifyNotFound(scheduler, PSCHED1_ID);
verifyNotFound(scheduler, TSCHED11_ID);
Assert.assertEquals(ImmutableList.of(), scheduler.listSchedules(PROG1_ID));
Assert.assertEquals(ImmutableList.of(), scheduler.listSchedules(PROG11_ID));
Assert.assertEquals(ImmutableList.of(psched2), scheduler.listSchedules(PROG2_ID));
Assert.assertEquals(ImmutableList.of(), scheduler.listSchedules(APP1_ID));
Assert.assertEquals(ImmutableList.of(psched2), scheduler.listSchedules(PROG2_ID));
}
use of co.cask.cdap.common.AlreadyExistsException in project cdap by caskdata.
the class CoreSchedulerService method updateSchedule.
@Override
public void updateSchedule(final ProgramSchedule schedule) throws NotFoundException, BadRequestException {
ProgramScheduleStatus previousStatus = getScheduleStatus(schedule.getScheduleId());
deleteSchedule(schedule.getScheduleId());
try {
addSchedule(schedule);
} catch (AlreadyExistsException e) {
// Should never reach here because we just deleted it
throw new IllegalStateException("Schedule '" + schedule.getScheduleId() + "' already exists despite just being deleted.");
}
// if the schedule was previously enabled, it should still/again enabled be after the update
if (ProgramScheduleStatus.SCHEDULED == previousStatus) {
try {
enableSchedule(schedule.getScheduleId());
} catch (ConflictException e) {
// Should never reach here because we just added this
throw new IllegalStateException("Schedule '" + schedule.getScheduleId() + "' already enabled despite just being added.");
}
}
}
use of co.cask.cdap.common.AlreadyExistsException in project cdap by caskdata.
the class NamespaceHttpHandler method create.
@PUT
@Path("/namespaces/{namespace-id}")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void create(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId) throws Exception {
Id.Namespace namespace;
try {
namespace = Id.Namespace.from(namespaceId);
} catch (IllegalArgumentException e) {
throw new BadRequestException("Namespace id can contain only alphanumeric characters or '_'.");
}
NamespaceMeta metadata = getNamespaceMeta(request);
if (isReserved(namespaceId)) {
throw new BadRequestException(String.format("Cannot create the namespace '%s'. '%s' is a reserved namespace.", namespaceId, namespaceId));
}
NamespaceMeta.Builder builder = metadata == null ? new NamespaceMeta.Builder() : new NamespaceMeta.Builder(metadata);
builder.setName(namespace);
NamespaceMeta finalMetadata = builder.build();
try {
namespaceAdmin.create(finalMetadata);
responder.sendString(HttpResponseStatus.OK, String.format("Namespace '%s' created successfully.", namespaceId));
} catch (AlreadyExistsException e) {
responder.sendString(HttpResponseStatus.OK, String.format("Namespace '%s' already exists.", namespaceId));
}
}
Aggregations