use of io.cdap.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), Collections.emptyList());
ProgramSchedule tsched11 = new ProgramSchedule("tsched11", "two times schedule", PROG11_ID, ImmutableMap.of("prop2", "xx"), new TimeTrigger("* * ? * 1,2"), Collections.emptyList());
ProgramSchedule psched2 = new ProgramSchedule("psched2", "two partition schedule", PROG2_ID, ImmutableMap.of("propper", "popper"), new PartitionTrigger(DS2_ID, 2), Collections.emptyList());
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 io.cdap.cdap.common.AlreadyExistsException in project cdap by caskdata.
the class NamespaceClientTestRun method testNamespaces.
@Test
public void testNamespaces() throws Exception {
List<NamespaceMeta> namespaces = namespaceClient.list();
int initialNamespaceCount = namespaces.size();
Assert.assertFalse(String.format("Namespace '%s' must not be found", DOES_NOT_EXIST), namespaceClient.exists(DOES_NOT_EXIST));
verifyReservedCreate();
verifyReservedDelete();
// create a valid namespace
NamespaceMeta.Builder builder = new NamespaceMeta.Builder();
builder.setName(TEST_NAMESPACE_NAME).setDescription(TEST_DESCRIPTION);
namespaceClient.create(builder.build());
waitForNamespaceCreation(TEST_NAMESPACE_NAME);
// verify that the namespace got created correctly
namespaces = namespaceClient.list();
Assert.assertEquals(initialNamespaceCount + 1, namespaces.size());
NamespaceMeta meta = namespaceClient.get(TEST_NAMESPACE_NAME);
Assert.assertEquals(TEST_NAMESPACE_NAME.getNamespace(), meta.getName());
Assert.assertEquals(TEST_DESCRIPTION, meta.getDescription());
// try creating a namespace with the same id again
builder.setName(TEST_NAMESPACE_NAME).setDescription("existing");
try {
namespaceClient.create(builder.build());
Assert.fail("Should not be able to re-create an existing namespace");
} catch (AlreadyExistsException e) {
// expected
}
// verify that the existing namespace was not updated
meta = namespaceClient.get(TEST_NAMESPACE_NAME);
Assert.assertEquals(TEST_NAMESPACE_NAME.getNamespace(), meta.getName());
Assert.assertEquals(TEST_DESCRIPTION, meta.getDescription());
// create and verify namespace without name and description
builder = new NamespaceMeta.Builder();
builder.setName(TEST_DEFAULT_FIELDS);
namespaceClient.create(builder.build());
namespaces = namespaceClient.list();
Assert.assertEquals(initialNamespaceCount + 2, namespaces.size());
meta = namespaceClient.get(TEST_DEFAULT_FIELDS);
Assert.assertEquals(TEST_DEFAULT_FIELDS.getNamespace(), meta.getName());
Assert.assertEquals("", meta.getDescription());
// cleanup
namespaceClient.delete(TEST_NAMESPACE_NAME);
namespaceClient.delete(TEST_DEFAULT_FIELDS);
Assert.assertEquals(initialNamespaceCount, namespaceClient.list().size());
}
use of io.cdap.cdap.common.AlreadyExistsException in project cdap by caskdata.
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 caskdata.
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 caskdata.
the class KMSSecureStoreService method put.
/**
* Stores an element in the secure store. The key is stored as namespace:name in the backing store,
* assuming ":" is the name separator.
* @param namespace The namespace this key belongs to.
* @param name Name of the element to store.
* @param data The data that needs to be securely stored.
* @param description User provided description of the entry.
* @param properties Metadata associated with the data
* @throws NamespaceNotFoundException If the specified namespace does not exist.
* @throws AlreadyExistsException If specified element already exists.
* @throws IOException If it failed to store the key in the store.
*/
// Unfortunately KeyProvider does not specify
// the underlying cause except in the message, so we can not throw a more specific exception.
@Override
public void put(String namespace, String name, String data, @Nullable String description, Map<String, String> properties) throws Exception {
checkNamespaceExists(namespace);
String keyName = getKeyName(namespace, name);
if (provider.getMetadata(keyName) != null) {
throw new AlreadyExistsException(String.format("Updating existing key %s is not supported.", name));
}
KeyProvider.Options options = new KeyProvider.Options(conf);
options.setDescription(description);
options.setAttributes(properties);
byte[] buff = data.getBytes(Charsets.UTF_8);
options.setBitLength(buff.length * Byte.SIZE);
try {
provider.createKey(keyName, buff, options);
} catch (IOException e) {
throw new IOException("Failed to store the key " + name + " under namespace " + namespace, e);
}
}
Aggregations