Search in sources :

Example 1 with AlreadyExistsException

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;
}
Also used : AlreadyExistsException(co.cask.cdap.common.AlreadyExistsException) ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) Get(co.cask.cdap.api.dataset.table.Get) Put(co.cask.cdap.api.dataset.table.Put) Constraint(co.cask.cdap.internal.schedule.constraint.Constraint)

Example 2 with AlreadyExistsException

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();
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) AlreadyExistsException(co.cask.cdap.common.AlreadyExistsException) ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) ApplicationId(co.cask.cdap.proto.id.ApplicationId) ScheduleSpecification(co.cask.cdap.api.schedule.ScheduleSpecification)

Example 3 with AlreadyExistsException

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();
    }
}
Also used : SecureStoreData(co.cask.cdap.api.security.store.SecureStoreData) SecureKeyId(co.cask.cdap.proto.id.SecureKeyId) AlreadyExistsException(co.cask.cdap.common.AlreadyExistsException) SecureStoreMetadata(co.cask.cdap.api.security.store.SecureStoreMetadata) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException)

Example 4 with AlreadyExistsException

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));
    }
}
Also used : AlreadyExistsException(co.cask.cdap.common.AlreadyExistsException) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) BadRequestException(co.cask.cdap.common.BadRequestException) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Id(co.cask.cdap.proto.Id) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Example 5 with AlreadyExistsException

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());
}
Also used : AlreadyExistsException(co.cask.cdap.common.AlreadyExistsException) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) Test(org.junit.Test)

Aggregations

AlreadyExistsException (co.cask.cdap.common.AlreadyExistsException)9 ProgramSchedule (co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule)3 Test (org.junit.Test)3 NotFoundException (co.cask.cdap.common.NotFoundException)2 NamespaceMeta (co.cask.cdap.proto.NamespaceMeta)2 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)1 Get (co.cask.cdap.api.dataset.table.Get)1 Put (co.cask.cdap.api.dataset.table.Put)1 TopicNotFoundException (co.cask.cdap.api.messaging.TopicNotFoundException)1 ScheduleSpecification (co.cask.cdap.api.schedule.ScheduleSpecification)1 SecureStoreData (co.cask.cdap.api.security.store.SecureStoreData)1 SecureStoreMetadata (co.cask.cdap.api.security.store.SecureStoreMetadata)1 BadRequestException (co.cask.cdap.common.BadRequestException)1 ConflictException (co.cask.cdap.common.ConflictException)1 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)1 ProgramScheduleStatus (co.cask.cdap.internal.app.runtime.schedule.ProgramScheduleStatus)1 PartitionTrigger (co.cask.cdap.internal.app.runtime.schedule.trigger.PartitionTrigger)1 TimeTrigger (co.cask.cdap.internal.app.runtime.schedule.trigger.TimeTrigger)1 Constraint (co.cask.cdap.internal.schedule.constraint.Constraint)1 Id (co.cask.cdap.proto.Id)1