private MDSKey getEntityIndexRowKeyPrefix(ProfileId profileId) { return getRowKey(INDEX_PREFIX, profileId.getNamespaceId(), profileId.getProfile(), null); }
@SuppressWarnings("unused") public static ProfileId fromIdParts(Iterable<String> idString) { Iterator<String> iterator = idString.iterator(); return new ProfileId(next(iterator, "namespace"), nextAndEnd(iterator, "profile")); }
/** * Delete the metrics for a profile. * * @param profileId the profile to delete metrics for. */ private void deleteMetrics(ProfileId profileId) { long endTs = System.currentTimeMillis() / 1000; Map<String, String> tags = new LinkedHashMap<>(); tags.put(Constants.Metrics.Tag.PROFILE_SCOPE, profileId.getScope().name()); tags.put(Constants.Metrics.Tag.PROFILE, profileId.getProfile()); // if the profile is in user scope, we need to add the namespace info to distinguish the profile if (profileId.getScope().equals(EntityScope.USER)) { tags.put(Constants.Metrics.Tag.NAMESPACE, profileId.getNamespace()); } MetricDeleteQuery deleteQuery = new MetricDeleteQuery(0, endTs, Collections.emptySet(), tags, new ArrayList<>(tags.keySet())); metricStore.delete(deleteQuery); } }
throw new ProfileConflictException( String.format("Profile %s in namespace %s is currently enabled. A profile can " + "only be deleted if it is disabled", profileId.getProfile(), profileId.getNamespace()), profileId); String.format("Profile '%s' is still assigned to %s%s. " + "Please delete all assignments before deleting the profile.", profileId.getProfile(), firstEntity, countStr), profileId); return scopedName != null && scopedName.equals(profileId.getScopedName()); }; if (profileId.getNamespaceId().equals(NamespaceId.SYSTEM)) { activeRuns = appMetadataStore.getActiveRuns(runRecordMetaPredicate); } else { activeRuns = appMetadataStore.getActiveRuns(Collections.singleton(profileId.getNamespaceId()), runRecordMetaPredicate); String.format("Profile '%s' is in use by run %s%s. Please stop all active runs " + "before deleting the profile.", profileId.toString(), firstRun, countStr), profileId);
protected void putProfile(ProfileId profileId, Object profile, int expectedCode) throws Exception { HttpResponse response = doPut(String.format("/v3/namespaces/%s/profiles/%s", profileId.getNamespace(), profileId.getProfile()), GSON.toJson(profile)); Assert.assertEquals(expectedCode, response.getResponseCode()); }
/** * Get the metrics context for the program, the tags are constructed with the program run id and * the profile id */ private MetricsContext getMetricsContextForProfile(MetricsCollectionService metricsCollectionService, ProgramRunId programRunId, ProfileId profileId) { Map<String, String> tags = ImmutableMap.<String, String>builder() .put(Constants.Metrics.Tag.PROFILE_SCOPE, profileId.getScope().name()) .put(Constants.Metrics.Tag.PROFILE, profileId.getProfile()) .put(Constants.Metrics.Tag.NAMESPACE, programRunId.getNamespace()) .put(Constants.Metrics.Tag.PROGRAM_TYPE, programRunId.getType().getPrettyName()) .put(Constants.Metrics.Tag.APP, programRunId.getApplication()) .put(Constants.Metrics.Tag.PROGRAM, programRunId.getProgram()) .put(Constants.Metrics.Tag.RUN_ID, programRunId.getRun()) .build(); return metricsCollectionService.getContext(tags); } }
private void addProfileMetadataUpdate(NamespacedEntityId entityId, ProfileId profileId, Map<MetadataEntity, Map<String, String>> updates) { LOG.trace("Setting profile metadata for {} to {}", entityId, profileId); updates.put(entityId.toMetadataEntity(), Collections.singletonMap(PROFILE_METADATA_KEY, profileId.getScopedName())); }
preferencesService.setProperties(NamespaceId.DEFAULT, Collections.singletonMap(SystemArguments.PROFILE_NAME, ProfileId.NATIVE.getScopedName())); 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()); 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()); Tasks.waitFor(ProfileId.NATIVE.getScopedName(), () -> mds.getProperties(workflowId.toMetadataEntity()).get("profile"), 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS); Tasks.waitFor(ProfileId.NATIVE.getScopedName(), () -> mds.getProperties(scheduleId.toMetadataEntity()).get("profile"), 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS); Tasks.waitFor(myProfile.getScopedName(), () -> mds.getProperties(workflowId.toMetadataEntity()).get("profile"), 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS); Tasks.waitFor(myProfile.getScopedName(), () -> mds.getProperties(scheduleId.toMetadataEntity()).get("profile"), 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS); ProfileId.NATIVE.getScopedName())); Tasks.waitFor(myProfile2.getScopedName(), () -> mds.getProperties(workflowId.toMetadataEntity()).get("profile"),
/** * Disable the profile. After the profile is disabled, any program/schedule cannot be associated with this profile. * * @param profileId the id of the profile to disable * @throws NotFoundException if the profile is not found * @throws ProfileConflictException if the profile is already disabled * @throws MethodNotAllowedException if trying to disable the native profile */ public void disableProfile(ProfileId profileId) throws NotFoundException, ProfileConflictException, MethodNotAllowedException { if (profileId.equals(ProfileId.NATIVE)) { throw new MethodNotAllowedException(String.format("Cannot change status for Profile Native %s, " + "it should always be ENABLED", profileId.getScopedName())); } Transactionals.execute(transactional, context -> { getProfileDataset(context).disableProfile(profileId); }, NotFoundException.class, ProfileConflictException.class); }
throw new ProfileConflictException(String.format("Profile %s in namespace %s is disabled. It cannot be " + "used to start the program %s", profileId.getProfile(), profileId.getNamespace(), programId.toString()), profileId); (ProfileId.NATIVE.equals(profileId) ? ClusterMode.ON_PREMISE : ClusterMode.ISOLATED).name()); ProgramSpecification programSpecification = getProgramSpecificationWithoutAuthz(programId); if (programSpecification == null) {
/** * Return which entity scope this profile is in * * @return entity scope the profile is in */ public EntityScope getScope() { return getNamespaceId().equals(NamespaceId.SYSTEM) ? EntityScope.SYSTEM : EntityScope.USER; }
@Test public void testNativeProfileImmutable() throws Exception { // verify native profile exists Assert.assertEquals(Profile.NATIVE, getSystemProfile(ProfileId.NATIVE.getProfile(), 200).get()); // disable, update, or delete should throw a 405 disableSystemProfile(ProfileId.NATIVE.getProfile(), 405); putSystemProfile(ProfileId.NATIVE.getProfile(), Profile.NATIVE, 405); deleteSystemProfile(ProfileId.NATIVE.getProfile(), 405); } }
@Test public void testGetProfileId() { // should get null profile id if the args is empty Assert.assertFalse(SystemArguments.getProfileIdFromArgs(NamespaceId.DEFAULT, Collections.emptyMap()).isPresent()); Map<String, String> args = new HashMap<>(); args.put("system.log.level", "DEBUG"); args.put("system.log.leveldummyKey", "ERROR"); // Having other unrelated args should also get null profile id Assert.assertFalse(SystemArguments.getProfileIdFromArgs(NamespaceId.DEFAULT, args).isPresent()); // without scope the profile will be considered in user scope ProfileId expected = NamespaceId.DEFAULT.profile("MyProfile"); args.put("system.profile.name", expected.getProfile()); Assert.assertEquals(expected, SystemArguments.getProfileIdFromArgs(NamespaceId.DEFAULT, args).get()); // put a profile with scope SYSTEM, the profile we get should be in system namespace expected = NamespaceId.SYSTEM.profile("MyProfile"); args.put("system.profile.name", expected.getScopedName()); Assert.assertEquals(expected, SystemArguments.getProfileIdFromArgs(NamespaceId.DEFAULT, args).get()); // put a profile with scope USER, the profile we get should be in the user namespace expected = NamespaceId.DEFAULT.profile("MyProfile"); args.put("system.profile.name", expected.getScopedName()); Assert.assertEquals(expected, SystemArguments.getProfileIdFromArgs(NamespaceId.DEFAULT, args).get()); } }
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()); Tasks.waitFor(myProfile.getScopedName(), () -> mds.getProperties(workflowId.toMetadataEntity()).get("profile"), 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS); Tasks.waitFor(ProfileId.NATIVE.getScopedName(), () -> mds.getProperties(workflowId.toMetadataEntity()).get("profile"), 10, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
public ProfileId profile(String profile) { return new ProfileId(namespace, profile); }
/** * Get the profile id from namespace and the argument. Note that the profile name in the args is initially a scoped * name. If it is in system scope, this method will return a profile id which is in SYSTEM namespace, otherwise, it * will be in the given namespace. * * @param namespaceId namespace this profile is in * @param args arguments to look up * @return the profile id if it is present */ public static Optional<ProfileId> getProfileIdFromArgs(NamespaceId namespaceId, Map<String, String> args) { if (args.containsKey(PROFILE_NAME)) { String scopedProfile = args.get(SystemArguments.PROFILE_NAME); ProfileId profileId = ProfileId.fromScopedName(namespaceId, scopedProfile); return Optional.of(profileId); } return Optional.empty(); }
public ProfileId(String namespace, String profileName) { super(namespace, EntityType.PROFILE); if (profileName == null) { throw new NullPointerException("Profile name cannot be null."); } ensureValidId("profile", profileName); this.profileName = profileName; }
throw new ProfileConflictException( String.format("Profile %s in namespace %s is currently enabled. A profile can " + "only be deleted if it is disabled", profileId.getProfile(), profileId.getNamespace()), profileId); String.format("Profile '%s' is still assigned to %s%s. " + "Please delete all assignments before deleting the profile.", profileId.getProfile(), firstEntity, countStr), profileId); return scopedName != null && scopedName.equals(profileId.getScopedName()); }; if (profileId.getNamespaceId().equals(NamespaceId.SYSTEM)) { activeRuns = appMetadataStore.getActiveRuns(runRecordMetaPredicate); } else { activeRuns = appMetadataStore.getActiveRuns(Collections.singleton(profileId.getNamespaceId()), runRecordMetaPredicate); String.format("Profile '%s' is in use by run %s%s. Please stop all active runs " + "before deleting the profile.", profileId.toString(), firstRun, countStr), profileId);
protected void disableProfile(ProfileId profileId, int expectedCode) throws Exception { HttpResponse response = doPost(String.format("/v3/namespaces/%s/profiles/%s/disable", profileId.getNamespace(), profileId.getProfile())); Assert.assertEquals(expectedCode, response.getResponseCode()); }
/** * Get the metrics context for the program, the tags are constructed with the program run id and * the profile id */ private MetricsContext getMetricsContextForProfile(MetricsCollectionService metricsCollectionService, ProgramRunId programRunId, ProfileId profileId) { Map<String, String> tags = ImmutableMap.<String, String>builder() .put(Constants.Metrics.Tag.PROFILE_SCOPE, profileId.getScope().name()) .put(Constants.Metrics.Tag.PROFILE, profileId.getProfile()) .put(Constants.Metrics.Tag.NAMESPACE, programRunId.getNamespace()) .put(Constants.Metrics.Tag.PROGRAM_TYPE, programRunId.getType().getPrettyName()) .put(Constants.Metrics.Tag.APP, programRunId.getApplication()) .put(Constants.Metrics.Tag.PROGRAM, programRunId.getProgram()) .put(Constants.Metrics.Tag.RUN_ID, programRunId.getRun()) .build(); return metricsCollectionService.getContext(tags); } }