use of io.cdap.cdap.common.AlreadyExistsException in project cdap by cdapio.
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 io.cdap.cdap.common.AlreadyExistsException in project cdap by cdapio.
the class ProgramScheduleStoreDataset method addScheduleWithStatus.
/**
* Add a schedule to the store.
*
* @param schedule the schedule to add
* @param status the status of the schedule to add
* @param currentTime the current time in milliseconds when adding the schedule
* @throws AlreadyExistsException if the schedule already exists
*/
private void addScheduleWithStatus(ProgramSchedule schedule, ProgramScheduleStatus status, long currentTime) throws AlreadyExistsException, IOException {
Collection<Field<?>> scheduleKeys = getScheduleKeys(schedule.getScheduleId());
Optional<StructuredRow> existing = scheduleStore.read(scheduleKeys);
if (existing.isPresent() && existing.get().getString(StoreDefinition.ProgramScheduleStore.SCHEDULE) != null) {
throw new AlreadyExistsException(schedule.getScheduleId());
}
Collection<Field<?>> scheduleFields = new ArrayList<>(scheduleKeys);
scheduleFields.add(Fields.stringField(StoreDefinition.ProgramScheduleStore.SCHEDULE, GSON.toJson(schedule)));
scheduleFields.add(Fields.longField(StoreDefinition.ProgramScheduleStore.UPDATE_TIME, currentTime));
scheduleFields.add(Fields.stringField(StoreDefinition.ProgramScheduleStore.STATUS, status.toString()));
scheduleStore.upsert(scheduleFields);
int count = 0;
for (String triggerKey : extractTriggerKeys(schedule)) {
Collection<Field<?>> triggerFields = getTriggerKeys(scheduleKeys, count++);
triggerFields.add(Fields.stringField(StoreDefinition.ProgramScheduleStore.TRIGGER_KEY, triggerKey));
triggerStore.upsert(triggerFields);
}
}
use of io.cdap.cdap.common.AlreadyExistsException in project cdap by cdapio.
the class ProgramScheduleStoreDataset method modifySchedulesTriggeredByDeletedProgram.
/**
* Update all schedules that can be triggered by the given deleted program. A schedule will be removed if
* the only {@link ProgramStatusTrigger} in it is triggered by the deleted program. Schedules with composite triggers
* will be updated if the composite trigger can still be satisfied after the program is deleted, otherwise the
* schedules will be deleted.
*
* @param programId the program id for which to delete the schedules
* @return the IDs of the schedules that were deleted
*/
public List<ProgramSchedule> modifySchedulesTriggeredByDeletedProgram(ProgramId programId) throws IOException {
long deleteTime = System.currentTimeMillis();
List<ProgramSchedule> deleted = new ArrayList<>();
Set<ProgramScheduleRecord> scheduleRecords = new HashSet<>();
for (ProgramStatus status : ProgramStatus.values()) {
scheduleRecords.addAll(findSchedules(Schedulers.triggerKeyForProgramStatus(programId, status)));
}
for (ProgramScheduleRecord scheduleRecord : scheduleRecords) {
ProgramSchedule schedule = scheduleRecord.getSchedule();
markScheduleAsDeleted(schedule.getScheduleId(), deleteTime);
triggerStore.deleteAll(Range.singleton(getScheduleKeys(schedule.getScheduleId())));
if (schedule.getTrigger() instanceof AbstractSatisfiableCompositeTrigger) {
// get the updated composite trigger by removing the program status trigger of the given program
Trigger updatedTrigger = ((AbstractSatisfiableCompositeTrigger) schedule.getTrigger()).getTriggerWithDeletedProgram(programId);
if (updatedTrigger == null) {
deleted.add(schedule);
continue;
}
// if the updated composite trigger is not null, add the schedule back with updated composite trigger
try {
addScheduleWithStatus(new ProgramSchedule(schedule.getName(), schedule.getDescription(), schedule.getProgramId(), schedule.getProperties(), updatedTrigger, schedule.getConstraints(), schedule.getTimeoutMillis()), scheduleRecord.getMeta().getStatus(), System.currentTimeMillis());
} catch (AlreadyExistsException e) {
// this should never happen
LOG.warn("Failed to add the schedule '{}' triggered by '{}' with updated trigger '{}', " + "skip adding this schedule.", schedule.getScheduleId(), programId, updatedTrigger, e);
}
} else {
deleted.add(schedule);
}
}
return deleted;
}
use of io.cdap.cdap.common.AlreadyExistsException in project cdap by cdapio.
the class CoreSchedulerService method updateSchedule.
@Override
public void updateSchedule(ProgramSchedule schedule) throws NotFoundException, BadRequestException, ProfileConflictException {
checkStarted();
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 io.cdap.cdap.common.AlreadyExistsException in project cdap by cdapio.
the class OwnerStoreTest method test.
@Test
public void test() throws Exception {
OwnerStore ownerStore = getOwnerStore();
DatasetId datasetId = NamespaceId.DEFAULT.dataset("fooData");
// No owner info should exist for above stream
Assert.assertNull(ownerStore.getOwner(datasetId));
// delete behavior is idempotent, so won't throw NotFoundException
ownerStore.delete(datasetId);
// Storing an owner for the first time should work
KerberosPrincipalId kerberosPrincipalId = new KerberosPrincipalId("alice/somehost@SOMEKDC.NET");
ownerStore.add(datasetId, kerberosPrincipalId);
// owner principal should exists
Assert.assertTrue(ownerStore.exists(datasetId));
// Should be able to get the principal back
Assert.assertEquals(kerberosPrincipalId, ownerStore.getOwner(datasetId));
// Should not be able to update the owner principal
try {
ownerStore.add(datasetId, new KerberosPrincipalId("bob@SOMEKDC.NET"));
Assert.fail();
} catch (AlreadyExistsException e) {
// expected
}
// Should not be able to update the owner principal
try {
ownerStore.add(datasetId, new KerberosPrincipalId("somePrincipal"));
Assert.fail();
} catch (AlreadyExistsException e) {
// expected
}
// trying to update with invalid principal should fail early on with IllegalArgumentException
try {
ownerStore.add(datasetId, new KerberosPrincipalId("b@ob@SOMEKDC.NET"));
Assert.fail();
} catch (IllegalArgumentException e) {
// expected
}
// Trying to store owner information for unsupported type should fail
try {
ownerStore.add(NamespaceId.DEFAULT.topic("anotherStream"), new KerberosPrincipalId("somePrincipal"));
Assert.fail();
} catch (IllegalArgumentException e) {
// expected
}
// delete the owner information
ownerStore.delete(datasetId);
Assert.assertFalse(ownerStore.exists(datasetId));
Assert.assertNull(ownerStore.getOwner(datasetId));
}
Aggregations