Search in sources :

Example 36 with ProfileId

use of io.cdap.cdap.proto.id.ProfileId in project cdap by cdapio.

the class ProfileServiceTest method testAddDeleteAssignments.

@Test
public void testAddDeleteAssignments() throws Exception {
    ProfileId myProfile = NamespaceId.DEFAULT.profile("MyProfile");
    Profile profile1 = new Profile("MyProfile", Profile.NATIVE.getLabel(), Profile.NATIVE.getDescription(), Profile.NATIVE.getScope(), Profile.NATIVE.getProvisioner());
    profileService.saveProfile(myProfile, profile1);
    // add a profile assignment and verify
    Set<EntityId> expected = new HashSet<>();
    expected.add(NamespaceId.DEFAULT);
    profileService.addProfileAssignment(myProfile, NamespaceId.DEFAULT);
    Assert.assertEquals(expected, profileService.getProfileAssignments(myProfile));
    // add more and verify
    InstanceId instanceId = new InstanceId("");
    ApplicationId myApp = NamespaceId.DEFAULT.app("myApp");
    ProgramId myProgram = myApp.workflow("myProgram");
    expected.add(instanceId);
    expected.add(myApp);
    expected.add(myProgram);
    profileService.addProfileAssignment(myProfile, instanceId);
    profileService.addProfileAssignment(myProfile, myApp);
    profileService.addProfileAssignment(myProfile, myProgram);
    Assert.assertEquals(expected, profileService.getProfileAssignments(myProfile));
    // add same entity id should not affect
    profileService.addProfileAssignment(myProfile, myApp);
    Assert.assertEquals(expected, profileService.getProfileAssignments(myProfile));
    // delete one and verify
    expected.remove(myApp);
    profileService.removeProfileAssignment(myProfile, myApp);
    Assert.assertEquals(expected, profileService.getProfileAssignments(myProfile));
    // delete all
    for (EntityId entityId : expected) {
        profileService.removeProfileAssignment(myProfile, entityId);
    }
    expected.clear();
    Assert.assertEquals(expected, profileService.getProfileAssignments(myProfile));
    // delete again should not affect
    profileService.removeProfileAssignment(myProfile, myApp);
    Assert.assertEquals(expected, profileService.getProfileAssignments(myProfile));
    profileService.disableProfile(myProfile);
    profileService.deleteProfile(myProfile);
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) EntityId(io.cdap.cdap.proto.id.EntityId) InstanceId(io.cdap.cdap.proto.id.InstanceId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramId(io.cdap.cdap.proto.id.ProgramId) Profile(io.cdap.cdap.proto.profile.Profile) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 37 with ProfileId

use of io.cdap.cdap.proto.id.ProfileId in project cdap by cdapio.

the class ProfileServiceTest method testProfileMetricsDeletion.

@Test
public void testProfileMetricsDeletion() throws Exception {
    ProfileId myProfile = NamespaceId.DEFAULT.profile("MyProfile");
    Profile profile = new Profile("MyProfile", Profile.NATIVE.getLabel(), Profile.NATIVE.getDescription(), Profile.NATIVE.getScope(), Profile.NATIVE.getProvisioner());
    ProgramRunId runId = NamespaceId.DEFAULT.app("myApp").workflow("myProgram").run(RunIds.generate());
    // create and disable the profile
    profileService.saveProfile(myProfile, profile);
    profileService.disableProfile(myProfile);
    // emit some metrics
    MetricsCollectionService metricService = getInjector().getInstance(MetricsCollectionService.class);
    MetricsContext metricsContext = metricService.getContext(getMetricsTags(runId, myProfile));
    metricsContext.increment(Constants.Metrics.Program.PROGRAM_NODE_MINUTES, 30L);
    MetricStore metricStore = getInjector().getInstance(MetricStore.class);
    Tasks.waitFor(30L, () -> getMetric(metricStore, runId, myProfile, "system." + Constants.Metrics.Program.PROGRAM_NODE_MINUTES), 10, TimeUnit.SECONDS);
    // delete and verify the metrics are gone
    profileService.deleteProfile(myProfile);
    Tasks.waitFor(0L, () -> getMetric(metricStore, runId, myProfile, "system." + Constants.Metrics.Program.PROGRAM_NODE_MINUTES), 10, TimeUnit.SECONDS);
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) MetricStore(io.cdap.cdap.api.metrics.MetricStore) MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService) MetricsContext(io.cdap.cdap.api.metrics.MetricsContext) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) Profile(io.cdap.cdap.proto.profile.Profile) Test(org.junit.Test)

Example 38 with ProfileId

use of io.cdap.cdap.proto.id.ProfileId in project cdap by cdapio.

the class ProfileServiceTest method testProfileDeletion.

@Test
public void testProfileDeletion() throws Exception {
    ProfileId myProfile = NamespaceId.DEFAULT.profile("MyProfile");
    ProfileId myProfile2 = NamespaceId.DEFAULT.profile("MyProfile2");
    Profile profile1 = new Profile("MyProfile", Profile.NATIVE.getLabel(), Profile.NATIVE.getDescription(), Profile.NATIVE.getScope(), Profile.NATIVE.getProvisioner());
    Profile profile2 = new Profile("MyProfile2", Profile.NATIVE.getLabel(), Profile.NATIVE.getDescription(), Profile.NATIVE.getScope(), ProfileStatus.DISABLED, Profile.NATIVE.getProvisioner());
    profileService.saveProfile(myProfile, profile1);
    // add profile2 and disable it, profile2 can get deleted at any time
    profileService.saveProfile(myProfile2, profile2);
    profileService.disableProfile(myProfile2);
    // Should not be able to delete because the profile is by default enabled
    try {
        profileService.deleteProfile(myProfile);
        Assert.fail();
    } catch (ProfileConflictException e) {
    // expected
    }
    try {
        profileService.deleteAllProfiles(NamespaceId.DEFAULT);
        Assert.fail();
    } catch (ProfileConflictException e) {
        // expected and check profile 2 is not getting deleted
        Assert.assertEquals(profile2, profileService.getProfile(myProfile2));
    }
    // add assignment and disable it, deletion should also fail
    profileService.addProfileAssignment(myProfile, NamespaceId.DEFAULT);
    profileService.disableProfile(myProfile);
    try {
        profileService.deleteProfile(myProfile);
        Assert.fail();
    } catch (ProfileConflictException e) {
    // expected
    }
    try {
        profileService.deleteAllProfiles(NamespaceId.DEFAULT);
        Assert.fail();
    } catch (ProfileConflictException e) {
        // expected and check profile 2 is not getting deleted
        Assert.assertEquals(profile2, profileService.getProfile(myProfile2));
    }
    profileService.removeProfileAssignment(myProfile, NamespaceId.DEFAULT);
    // add an active record to DefaultStore, deletion should still fail
    Store store = getDefaultStore();
    ProgramId programId = NamespaceId.DEFAULT.app("myApp").workflow("myProgram");
    ArtifactId artifactId = NamespaceId.DEFAULT.artifact("testArtifact", "1.0").toApiArtifactId();
    RunId runId = RunIds.generate(System.currentTimeMillis());
    ProgramRunId programRunId = programId.run(runId.getId());
    Map<String, String> systemArgs = Collections.singletonMap(SystemArguments.PROFILE_NAME, myProfile.getScopedName());
    int sourceId = 0;
    store.setProvisioning(programRunId, Collections.emptyMap(), systemArgs, AppFabricTestHelper.createSourceId(++sourceId), artifactId);
    store.setProvisioned(programRunId, 0, AppFabricTestHelper.createSourceId(++sourceId));
    store.setStart(programRunId, null, systemArgs, AppFabricTestHelper.createSourceId(++sourceId));
    try {
        profileService.deleteProfile(myProfile);
        Assert.fail();
    } catch (ProfileConflictException e) {
    // expected
    }
    try {
        profileService.deleteAllProfiles(NamespaceId.DEFAULT);
        Assert.fail();
    } catch (ProfileConflictException e) {
        // expected and check profile 2 is not getting deleted
        Assert.assertEquals(profile2, profileService.getProfile(myProfile2));
    }
    // set the run to stopped then deletion should work
    store.setStop(programRunId, System.currentTimeMillis() + 1000, ProgramController.State.ERROR.getRunStatus(), AppFabricTestHelper.createSourceId(++sourceId));
    // now profile deletion should succeed
    profileService.deleteProfile(myProfile);
    Assert.assertEquals(Collections.singletonList(profile2), profileService.getProfiles(NamespaceId.DEFAULT, false));
    profileService.saveProfile(myProfile, profile1);
    profileService.disableProfile(myProfile);
    profileService.deleteAllProfiles(NamespaceId.DEFAULT);
    Assert.assertEquals(Collections.emptyList(), profileService.getProfiles(NamespaceId.DEFAULT, false));
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) MetricStore(io.cdap.cdap.api.metrics.MetricStore) Store(io.cdap.cdap.app.store.Store) DefaultStore(io.cdap.cdap.internal.app.store.DefaultStore) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) ProgramId(io.cdap.cdap.proto.id.ProgramId) RunId(org.apache.twill.api.RunId) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) ProfileConflictException(io.cdap.cdap.common.ProfileConflictException) Profile(io.cdap.cdap.proto.profile.Profile) Test(org.junit.Test)

Example 39 with ProfileId

use of io.cdap.cdap.proto.id.ProfileId in project cdap by cdapio.

the class ProfileMetadataMessageProcessor method collectProgramProfileMetadata.

private void collectProgramProfileMetadata(ProgramId programId, @Nullable ProfileId appProfile, List<MetadataMutation> updates) throws IOException {
    ProfileId programProfile = appProfile == null ? getResolvedProfileId(programId) : getProfileId(programId).orElse(appProfile);
    addProfileMetadataUpdate(programId, programProfile, updates);
    for (ProgramSchedule schedule : scheduleDataset.listSchedules(programId)) {
        collectScheduleProfileMetadata(schedule, programProfile, updates);
    }
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) ProgramSchedule(io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule)

Example 40 with ProfileId

use of io.cdap.cdap.proto.id.ProfileId in project cdap by cdapio.

the class ProgramNotificationSubscriberServiceTest method testMetricsEmit.

@Test
public void testMetricsEmit() throws Exception {
    ProfileService profileService = injector.getInstance(ProfileService.class);
    // create my profile
    ProfileId myProfile = NamespaceId.DEFAULT.profile("MyProfile");
    profileService.saveProfile(myProfile, Profile.NATIVE);
    ProgramId programId = NamespaceId.DEFAULT.app("myApp").workflow("myProgram");
    ArtifactId artifactId = NamespaceId.DEFAULT.artifact("testArtifact", "1.0").toApiArtifactId();
    RunId runId = RunIds.generate(System.currentTimeMillis());
    ProgramRunId programRunId = programId.run(runId.getId());
    Map<String, String> systemArgs = Collections.singletonMap(SystemArguments.PROFILE_NAME, myProfile.getScopedName());
    long startTime = System.currentTimeMillis();
    // record the program status in app meta store
    TransactionRunners.run(transactionRunner, context -> {
        AppMetadataStore metadataStoreDataset = AppMetadataStore.create(context);
        int sourceId = 0;
        metadataStoreDataset.recordProgramProvisioning(programRunId, Collections.emptyMap(), systemArgs, AppFabricTestHelper.createSourceId(++sourceId), artifactId);
        // using 3 nodes
        metadataStoreDataset.recordProgramProvisioned(programRunId, 3, AppFabricTestHelper.createSourceId(++sourceId));
        metadataStoreDataset.recordProgramStart(programRunId, null, systemArgs, AppFabricTestHelper.createSourceId(++sourceId));
        metadataStoreDataset.recordProgramRunning(programRunId, startTime + 60000, null, AppFabricTestHelper.createSourceId(++sourceId));
    });
    programStateWriter.completed(programRunId);
    MetricStore metricStore = injector.getInstance(MetricStore.class);
    Map<String, String> additionalTags = getAdditionalTagsForProgramMetrics(ProgramRunStatus.RUNNING, null, ProgramRunClusterStatus.PROVISIONED);
    Tasks.waitFor(1L, () -> getMetric(metricStore, programRunId, myProfile, additionalTags, SYSTEM_METRIC_PREFIX + Constants.Metrics.Program.PROGRAM_COMPLETED_RUNS), 10, TimeUnit.SECONDS);
    // verify the metrics
    Assert.assertEquals(1L, getMetric(metricStore, programRunId, myProfile, additionalTags, SYSTEM_METRIC_PREFIX + Constants.Metrics.Program.PROGRAM_COMPLETED_RUNS));
    Assert.assertEquals(0L, getMetric(metricStore, programRunId, myProfile, additionalTags, SYSTEM_METRIC_PREFIX + Constants.Metrics.Program.PROGRAM_KILLED_RUNS));
    Assert.assertEquals(0L, getMetric(metricStore, programRunId, myProfile, additionalTags, SYSTEM_METRIC_PREFIX + Constants.Metrics.Program.PROGRAM_FAILED_RUNS));
    metricStore.deleteAll();
}
Also used : ProfileId(io.cdap.cdap.proto.id.ProfileId) MetricStore(io.cdap.cdap.api.metrics.MetricStore) ProfileService(io.cdap.cdap.internal.profile.ProfileService) AppMetadataStore(io.cdap.cdap.internal.app.store.AppMetadataStore) ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) ProgramId(io.cdap.cdap.proto.id.ProgramId) RunId(org.apache.twill.api.RunId) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) Test(org.junit.Test)

Aggregations

ProfileId (io.cdap.cdap.proto.id.ProfileId)112 Test (org.junit.Test)68 Profile (io.cdap.cdap.proto.profile.Profile)50 ProgramId (io.cdap.cdap.proto.id.ProgramId)26 ProvisionerInfo (io.cdap.cdap.proto.provisioner.ProvisionerInfo)26 Path (javax.ws.rs.Path)22 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)16 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)14 HashMap (java.util.HashMap)14 ProfileConflictException (io.cdap.cdap.common.ProfileConflictException)12 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)12 MetricStore (io.cdap.cdap.api.metrics.MetricStore)10 NotFoundException (io.cdap.cdap.common.NotFoundException)10 ProgramSchedule (io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule)10 ProfileService (io.cdap.cdap.internal.profile.ProfileService)10 ArrayList (java.util.ArrayList)10 MetricsCollectionService (io.cdap.cdap.api.metrics.MetricsCollectionService)8 ScheduleId (io.cdap.cdap.proto.id.ScheduleId)8 POST (javax.ws.rs.POST)8 Store (io.cdap.cdap.app.store.Store)6