Search in sources :

Example 6 with AlreadyExistsException

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);
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) AlreadyExistsException(io.cdap.cdap.common.AlreadyExistsException) HttpResponse(io.cdap.common.http.HttpResponse) NotFoundException(io.cdap.cdap.common.NotFoundException) URL(java.net.URL)

Example 7 with AlreadyExistsException

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);
    }
}
Also used : Field(io.cdap.cdap.spi.data.table.field.Field) AlreadyExistsException(io.cdap.cdap.common.AlreadyExistsException) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) ArrayList(java.util.ArrayList) Constraint(io.cdap.cdap.internal.schedule.constraint.Constraint)

Example 8 with AlreadyExistsException

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;
}
Also used : ProgramStatusTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.ProgramStatusTrigger) SatisfiableTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.SatisfiableTrigger) Trigger(io.cdap.cdap.api.schedule.Trigger) AbstractSatisfiableCompositeTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.AbstractSatisfiableCompositeTrigger) AlreadyExistsException(io.cdap.cdap.common.AlreadyExistsException) ProgramSchedule(io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule) ArrayList(java.util.ArrayList) AbstractSatisfiableCompositeTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.AbstractSatisfiableCompositeTrigger) ProgramScheduleRecord(io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleRecord) HashSet(java.util.HashSet) ProgramStatus(io.cdap.cdap.api.ProgramStatus)

Example 9 with AlreadyExistsException

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.");
        }
    }
}
Also used : ProgramScheduleStatus(io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleStatus) AlreadyExistsException(io.cdap.cdap.common.AlreadyExistsException) ProfileConflictException(io.cdap.cdap.common.ProfileConflictException) ConflictException(io.cdap.cdap.common.ConflictException)

Example 10 with AlreadyExistsException

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));
}
Also used : AlreadyExistsException(io.cdap.cdap.common.AlreadyExistsException) KerberosPrincipalId(io.cdap.cdap.proto.id.KerberosPrincipalId) OwnerStore(io.cdap.cdap.security.impersonation.OwnerStore) DatasetId(io.cdap.cdap.proto.id.DatasetId) Test(org.junit.Test)

Aggregations

AlreadyExistsException (io.cdap.cdap.common.AlreadyExistsException)18 Test (org.junit.Test)6 NotFoundException (io.cdap.cdap.common.NotFoundException)4 ProgramSchedule (io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule)4 NamespaceMeta (io.cdap.cdap.proto.NamespaceMeta)4 ArrayList (java.util.ArrayList)4 ProgramStatus (io.cdap.cdap.api.ProgramStatus)2 Trigger (io.cdap.cdap.api.schedule.Trigger)2 BadRequestException (io.cdap.cdap.common.BadRequestException)2 ConflictException (io.cdap.cdap.common.ConflictException)2 ProfileConflictException (io.cdap.cdap.common.ProfileConflictException)2 AuditPolicy (io.cdap.cdap.common.security.AuditPolicy)2 ProgramScheduleRecord (io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleRecord)2 ProgramScheduleStatus (io.cdap.cdap.internal.app.runtime.schedule.ProgramScheduleStatus)2 AbstractSatisfiableCompositeTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.AbstractSatisfiableCompositeTrigger)2 PartitionTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.PartitionTrigger)2 ProgramStatusTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.ProgramStatusTrigger)2 SatisfiableTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.SatisfiableTrigger)2 TimeTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.TimeTrigger)2 Constraint (io.cdap.cdap.internal.schedule.constraint.Constraint)2