use of co.cask.cdap.common.AlreadyExistsException in project cdap by caskdata.
the class ProgramScheduleStoreDataset method addSchedules.
/**
* Add one or more schedules to the store.
*
* @param schedules the schedules to add
* @return the new schedules' last modified timestamp
* @throws AlreadyExistsException if one of the schedules already exists
*/
public long addSchedules(Iterable<? extends ProgramSchedule> schedules) throws AlreadyExistsException {
long currentTime = System.currentTimeMillis();
for (ProgramSchedule schedule : schedules) {
byte[] scheduleKey = rowKeyBytesForSchedule(schedule.getProgramId().getParent().schedule(schedule.getName()));
if (!store.get(new Get(scheduleKey)).isEmpty()) {
throw new AlreadyExistsException(schedule.getProgramId().getParent().schedule(schedule.getName()));
}
Put schedulePut = new Put(scheduleKey);
schedulePut.add(SCHEDULE_COLUMN_BYTES, GSON.toJson(schedule));
schedulePut.add(UPDATED_COLUMN_BYTES, currentTime);
// initially suspended
schedulePut.add(STATUS_COLUMN_BYTES, ProgramScheduleStatus.SUSPENDED.toString());
store.put(schedulePut);
int count = 0;
for (String triggerKey : extractTriggerKeys(schedule)) {
byte[] triggerRowKey = rowKeyBytesForTrigger(scheduleKey, count++);
store.put(new Put(triggerRowKey, TRIGGER_KEY_COLUMN_BYTES, triggerKey));
}
}
return currentTime;
}
use of co.cask.cdap.common.AlreadyExistsException in project cdap by caskdata.
the class ProgramScheduleStoreDataset method migrateFromAppMetadataStore.
/**
* Migrate schedules in a given namespace from app metadata store to ProgramScheduleStoreDataset
*
* @param namespaceId the namespace with schedules to be migrated
* @param appMetaStore app metadata store with schedules to be migrated
* @return the lexicographically largest namespace id String with schedule migration completed
*/
public String migrateFromAppMetadataStore(NamespaceId namespaceId, Store appMetaStore) {
String completedNamespace = getMigrationCompleteNamespace();
if (completedNamespace != null && completedNamespace.compareTo(namespaceId.toString()) > 0) {
return completedNamespace;
}
for (ApplicationSpecification appSpec : appMetaStore.getAllApplications(namespaceId)) {
ApplicationId appId = namespaceId.app(appSpec.getName(), appSpec.getAppVersion());
for (ScheduleSpecification scheduleSpec : appSpec.getSchedules().values()) {
ProgramSchedule schedule = Schedulers.toProgramSchedule(appId, scheduleSpec);
try {
addSchedule(schedule);
} catch (AlreadyExistsException e) {
// This should never happen since no schedule with the same schedule key should exist before migration
LOG.warn("Schedule {} already exists before schedule migration", schedule.getScheduleId(), e);
}
}
}
store.put(MIGRATION_COMPLETE_NAMESPACE_ROW_BYTES, MIGRATION_COMPLETE_NAMESPACE_COLUMN_BYTES, Bytes.toBytes(namespaceId.toString()));
return namespaceId.toString();
}
use of co.cask.cdap.common.AlreadyExistsException in project cdap by caskdata.
the class FileSecureStore method putSecureData.
/**
* Stores an element in the secure store. Although JCEKS supports overwriting keys the interface currently does not
* support it. If the key already exists then this method throws an AlreadyExistsException.
* @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 the key already exists in the namespace. Updating is not supported.
* @throws IOException If there was a problem storing the key to the in memory keystore
* or if there was problem persisting the keystore.
*/
@Override
public void putSecureData(String namespace, String name, String data, String description, Map<String, String> properties) throws Exception {
checkNamespaceExists(namespace);
String keyName = getKeyName(namespace, name);
SecureStoreMetadata meta = SecureStoreMetadata.of(name, description, properties);
SecureStoreData secureStoreData = new SecureStoreData(meta, data.getBytes(Charsets.UTF_8));
writeLock.lock();
try {
if (keyStore.containsAlias(keyName)) {
throw new AlreadyExistsException(new SecureKeyId(namespace, name));
}
keyStore.setKeyEntry(keyName, new KeyStoreEntry(secureStoreData, meta), password, null);
// Attempt to persist the store.
flush();
LOG.debug(String.format("Successfully stored %s in namespace %s", name, namespace));
} catch (KeyStoreException e) {
// We failed to store the key in the key store. Throw an IOException.
throw new IOException("Failed to store the key. ", e);
} finally {
writeLock.unlock();
}
}
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));
}
}
use of co.cask.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());
}
Aggregations