Search in sources :

Example 6 with ScheduleId

use of io.cdap.cdap.proto.id.ScheduleId in project cdap by caskdata.

the class MetadataSubscriberServiceTest method testProfileMetadata.

@Test
public void testProfileMetadata() throws Exception {
    Injector injector = getInjector();
    ApplicationSpecification appSpec = Specifications.from(new AppWithWorkflow());
    ApplicationId appId = NamespaceId.DEFAULT.app(appSpec.getName());
    ProgramId workflowId = appId.workflow("SampleWorkflow");
    ScheduleId scheduleId = appId.schedule("tsched1");
    // publish a creation of a schedule that will never exist
    // this tests that such a message is eventually discarded
    // note that for this test, we configure a fast retry strategy and a small number of retries
    // therefore this will cost only a few seconds delay
    publishBogusCreationEvent();
    // get the mds should be empty property since we haven't started the MetadataSubscriberService
    MetadataStorage mds = injector.getInstance(MetadataStorage.class);
    Assert.assertEquals(Collections.emptyMap(), mds.read(new Read(workflowId.toMetadataEntity())).getProperties());
    Assert.assertEquals(Collections.emptyMap(), mds.read(new Read(scheduleId.toMetadataEntity())).getProperties());
    // add a app with workflow to app meta store
    // note: since we bypass the app-fabric when adding this app, no ENTITY_CREATION message
    // will be published for the app (it happens in app lifecycle service). Therefore this
    // app must exist before assigning the profile for the namespace, otherwise the app's
    // programs will not receive the profile metadata.
    Store store = injector.getInstance(DefaultStore.class);
    store.addApplication(appId, appSpec);
    // set default namespace to use the profile, since now MetadataSubscriberService is not started,
    // it should not affect the mds
    PreferencesService preferencesService = injector.getInstance(PreferencesService.class);
    preferencesService.setProperties(NamespaceId.DEFAULT, Collections.singletonMap(SystemArguments.PROFILE_NAME, ProfileId.NATIVE.getScopedName()));
    // add a schedule to schedule store
    ProgramScheduleService scheduleService = injector.getInstance(ProgramScheduleService.class);
    scheduleService.add(new ProgramSchedule("tsched1", "one time schedule", workflowId, Collections.emptyMap(), new TimeTrigger("* * ? * 1"), ImmutableList.of()));
    // add a new profile in default namespace
    ProfileService profileService = injector.getInstance(ProfileService.class);
    ProfileId myProfile = new ProfileId(NamespaceId.DEFAULT.getNamespace(), "MyProfile");
    Profile profile1 = new Profile("MyProfile", Profile.NATIVE.getLabel(), Profile.NATIVE.getDescription(), Profile.NATIVE.getScope(), Profile.NATIVE.getProvisioner());
    profileService.saveProfile(myProfile, profile1);
    // add a second profile in default namespace
    ProfileId myProfile2 = new ProfileId(NamespaceId.DEFAULT.getNamespace(), "MyProfile2");
    Profile profile2 = new Profile("MyProfile2", Profile.NATIVE.getLabel(), Profile.NATIVE.getDescription(), Profile.NATIVE.getScope(), Profile.NATIVE.getProvisioner());
    profileService.saveProfile(myProfile2, profile2);
    try {
        // Verify the workflow profile metadata is updated to default profile
        Tasks.waitFor(ProfileId.NATIVE.getScopedName(), () -> getProfileProperty(mds, workflowId), 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
        // Verify the schedule profile metadata is updated to default profile
        Tasks.waitFor(ProfileId.NATIVE.getScopedName(), () -> getProfileProperty(mds, scheduleId), 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
        // set default namespace to use my profile
        preferencesService.setProperties(NamespaceId.DEFAULT, Collections.singletonMap(SystemArguments.PROFILE_NAME, "USER:MyProfile"));
        // Verify the workflow profile metadata is updated to my profile
        Tasks.waitFor(myProfile.getScopedName(), () -> getProfileProperty(mds, workflowId), 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
        // Verify the schedule profile metadata is updated to my profile
        Tasks.waitFor(myProfile.getScopedName(), () -> getProfileProperty(mds, scheduleId), 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
        // set app level to use my profile 2
        preferencesService.setProperties(appId, Collections.singletonMap(SystemArguments.PROFILE_NAME, "USER:MyProfile2"));
        // set instance level to system profile
        preferencesService.setProperties(Collections.singletonMap(SystemArguments.PROFILE_NAME, ProfileId.NATIVE.getScopedName()));
        // Verify the workflow profile metadata is updated to MyProfile2 which is at app level
        Tasks.waitFor(myProfile2.getScopedName(), () -> getProfileProperty(mds, workflowId), 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
        // Verify the schedule profile metadata is updated to MyProfile2 which is at app level
        Tasks.waitFor(myProfile2.getScopedName(), () -> getProfileProperty(mds, scheduleId), 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
        // remove the preferences at instance level, should not affect the metadata
        preferencesService.deleteProperties();
        // Verify the workflow profile metadata is updated to default profile
        Tasks.waitFor(myProfile2.getScopedName(), () -> getProfileProperty(mds, workflowId), 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
        // Verify the schedule profile metadata is updated to default profile
        Tasks.waitFor(myProfile2.getScopedName(), () -> getProfileProperty(mds, scheduleId), 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
        // remove app level pref should let the programs/schedules use ns level pref
        preferencesService.deleteProperties(appId);
        // Verify the workflow profile metadata is updated to MyProfile
        Tasks.waitFor(myProfile.getScopedName(), () -> getProfileProperty(mds, workflowId), 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
        // Verify the schedule profile metadata is updated to MyProfile
        Tasks.waitFor(myProfile.getScopedName(), () -> getProfileProperty(mds, scheduleId), 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
        // remove ns level pref so no pref is there
        preferencesService.deleteProperties(NamespaceId.DEFAULT);
        // Verify the workflow profile metadata is updated to default profile
        Tasks.waitFor(ProfileId.NATIVE.getScopedName(), () -> getProfileProperty(mds, workflowId), 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
        // Verify the schedule profile metadata is updated to default profile
        Tasks.waitFor(ProfileId.NATIVE.getScopedName(), () -> getProfileProperty(mds, scheduleId), 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
    } finally {
        // stop and clean up the store
        preferencesService.deleteProperties(NamespaceId.DEFAULT);
        preferencesService.deleteProperties();
        preferencesService.deleteProperties(appId);
        store.removeAll(NamespaceId.DEFAULT);
        scheduleService.delete(scheduleId);
        profileService.disableProfile(myProfile);
        profileService.disableProfile(myProfile2);
        profileService.deleteAllProfiles(myProfile.getNamespaceId());
        mds.apply(new MetadataMutation.Drop(workflowId.toMetadataEntity()), MutationOptions.DEFAULT);
        mds.apply(new MetadataMutation.Drop(scheduleId.toMetadataEntity()), MutationOptions.DEFAULT);
    }
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ApplicationSpecification(io.cdap.cdap.api.app.ApplicationSpecification) TimeTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.TimeTrigger) DefaultStore(io.cdap.cdap.internal.app.store.DefaultStore) Store(io.cdap.cdap.app.store.Store) ProgramId(io.cdap.cdap.proto.id.ProgramId) ScheduleId(io.cdap.cdap.proto.id.ScheduleId) AppWithWorkflow(io.cdap.cdap.AppWithWorkflow) Profile(io.cdap.cdap.proto.profile.Profile) PreferencesService(io.cdap.cdap.config.PreferencesService) Read(io.cdap.cdap.spi.metadata.Read) MetadataMutation(io.cdap.cdap.spi.metadata.MetadataMutation) ProfileService(io.cdap.cdap.internal.profile.ProfileService) ProgramSchedule(io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule) Injector(com.google.inject.Injector) MetadataStorage(io.cdap.cdap.spi.metadata.MetadataStorage) ProgramScheduleService(io.cdap.cdap.scheduler.ProgramScheduleService) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Test(org.junit.Test)

Example 7 with ScheduleId

use of io.cdap.cdap.proto.id.ScheduleId in project cdap by caskdata.

the class CoreSchedulerServiceTest method enableSchedule.

private void enableSchedule(String name) throws NotFoundException, ConflictException {
    ScheduleId scheduleId = APP_ID.schedule(name);
    scheduler.enableSchedule(scheduleId);
    Assert.assertEquals(ProgramScheduleStatus.SCHEDULED, scheduler.getScheduleStatus(scheduleId));
}
Also used : ScheduleId(io.cdap.cdap.proto.id.ScheduleId)

Example 8 with ScheduleId

use of io.cdap.cdap.proto.id.ScheduleId in project cdap by caskdata.

the class CoreSchedulerServiceTest method disableSchedule.

private void disableSchedule(String name) throws NotFoundException, ConflictException {
    ScheduleId scheduleId = APP_ID.schedule(name);
    scheduler.disableSchedule(scheduleId);
    Assert.assertEquals(ProgramScheduleStatus.SUSPENDED, scheduler.getScheduleStatus(scheduleId));
}
Also used : ScheduleId(io.cdap.cdap.proto.id.ScheduleId)

Example 9 with ScheduleId

use of io.cdap.cdap.proto.id.ScheduleId in project cdap by caskdata.

the class ScheduleFetcherTest method testGetScheduleNotFound.

@Test(expected = ScheduleNotFoundException.class)
public void testGetScheduleNotFound() throws Exception {
    ScheduleFetcher fetcher = getScheduleFetcher(fetcherType);
    String namespace = TEST_NAMESPACE1;
    String appName = AllProgramsApp.NAME;
    // Deploy the application.
    deploy(AllProgramsApp.class, 200, Constants.Gateway.API_VERSION_3_TOKEN, namespace);
    // Get and validate the schedule
    ScheduleId scheduleId = new ScheduleId(namespace, appName, "InvalidSchedule");
    try {
        ScheduleDetail scheduleDetail = fetcher.get(scheduleId);
    } finally {
        // Delete the application
        Assert.assertEquals(200, doDelete(getVersionedAPIPath("apps/", Constants.Gateway.API_VERSION_3_TOKEN, namespace)).getResponseCode());
    }
}
Also used : ScheduleDetail(io.cdap.cdap.proto.ScheduleDetail) ScheduleId(io.cdap.cdap.proto.id.ScheduleId) Test(org.junit.Test)

Example 10 with ScheduleId

use of io.cdap.cdap.proto.id.ScheduleId in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method doPerformAction.

private void doPerformAction(FullHttpRequest request, HttpResponder responder, String namespaceId, String appId, String appVersion, String type, String programId, String action) throws Exception {
    ApplicationId applicationId = new ApplicationId(namespaceId, appId, appVersion);
    if (SCHEDULES.equals(type)) {
        ScheduleId scheduleId = applicationId.schedule(programId);
        if (action.equals("disable") || action.equals("suspend")) {
            programScheduleService.suspend(scheduleId);
        } else if (action.equals("enable") || action.equals("resume")) {
            programScheduleService.resume(scheduleId);
        } else {
            throw new BadRequestException("Action for schedules may only be 'enable', 'disable', 'suspend', or 'resume' but is'" + action + "'");
        }
        responder.sendJson(HttpResponseStatus.OK, "OK");
        return;
    }
    ProgramType programType = getProgramType(type);
    ProgramId program = applicationId.program(programType, programId);
    Map<String, String> args = decodeArguments(request);
    // we have already validated that the action is valid
    switch(action.toLowerCase()) {
        case "start":
            lifecycleService.run(program, args, false);
            break;
        case "debug":
            if (!isDebugAllowed(programType)) {
                throw new NotImplementedException(String.format("debug action is not implemented for program type %s", programType));
            }
            lifecycleService.run(program, args, true);
            break;
        case "stop":
            lifecycleService.stop(program);
            break;
        default:
            throw new NotFoundException(String.format("%s action was not found", action));
    }
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : NotImplementedException(io.cdap.cdap.common.NotImplementedException) BadRequestException(io.cdap.cdap.common.BadRequestException) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException) ProgramType(io.cdap.cdap.proto.ProgramType) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ScheduleId(io.cdap.cdap.proto.id.ScheduleId) ProgramId(io.cdap.cdap.proto.id.ProgramId)

Aggregations

ScheduleId (io.cdap.cdap.proto.id.ScheduleId)66 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)36 ProgramId (io.cdap.cdap.proto.id.ProgramId)20 ScheduleDetail (io.cdap.cdap.proto.ScheduleDetail)18 Test (org.junit.Test)16 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)14 CommandInputError (io.cdap.cdap.cli.exception.CommandInputError)12 NotFoundException (io.cdap.cdap.common.NotFoundException)12 ScheduleProgramInfo (io.cdap.cdap.api.workflow.ScheduleProgramInfo)10 ProgramSchedule (io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule)10 ProfileId (io.cdap.cdap.proto.id.ProfileId)10 ApplicationSpecification (io.cdap.cdap.api.app.ApplicationSpecification)8 ProgramType (io.cdap.cdap.proto.ProgramType)8 StructuredRow (io.cdap.cdap.spi.data.StructuredRow)8 TimeTrigger (io.cdap.cdap.internal.app.runtime.schedule.trigger.TimeTrigger)6 Constraint (io.cdap.cdap.internal.schedule.constraint.Constraint)6 ProtoTrigger (io.cdap.cdap.proto.ProtoTrigger)6 AppRequest (io.cdap.cdap.proto.artifact.AppRequest)6 ApplicationManager (io.cdap.cdap.test.ApplicationManager)5 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)4