Skip to content

Commit

Permalink
Merge pull request #1268 from shreyabiradar07/metricProfile-with-crea…
Browse files Browse the repository at this point in the history
…teExp

Integrate MetricProfile with local Experiments
  • Loading branch information
chandrams authored Aug 26, 2024
2 parents d13ff9b + 363a297 commit cc2da86
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.autotune.common.data.result.ContainerData;
import com.autotune.common.k8sObjects.K8sObject;
import com.autotune.database.service.ExperimentDBService;
import com.autotune.operator.KruizeDeploymentInfo;
import com.autotune.operator.KruizeOperator;
import com.autotune.utils.KruizeConstants;
import org.slf4j.Logger;
Expand Down Expand Up @@ -105,9 +106,13 @@ public void validate(List<KruizeObject> kruizeExptList) {
errorMsg = AnalyzerErrorConstants.AutotuneObjectErrors.SLO_REDUNDANCY_ERROR;
validationOutputData.setErrorCode(HttpServletResponse.SC_BAD_REQUEST);
} else {
// fetch the Performance Profile from the DB
// fetch the Performance / Metric Profile from the DB
try {
new ExperimentDBService().loadPerformanceProfileFromDBByName(performanceProfilesMap, kruizeObject.getPerformanceProfile());
if (!KruizeDeploymentInfo.local) {
new ExperimentDBService().loadPerformanceProfileFromDBByName(performanceProfilesMap, kruizeObject.getPerformanceProfile());
} else {
new ExperimentDBService().loadMetricProfileFromDBByName(performanceProfilesMap, kruizeObject.getPerformanceProfile());
}
} catch (Exception e) {
LOGGER.error("Loading saved Performance Profile {} failed: {} ", expName, e.getMessage());
}
Expand All @@ -122,8 +127,12 @@ public void validate(List<KruizeObject> kruizeExptList) {
errorMsg = AnalyzerErrorConstants.AutotuneObjectErrors.MISSING_SLO_DATA;
validationOutputData.setErrorCode(HttpServletResponse.SC_BAD_REQUEST);
} else {
// TODO - set metric profile when local=true
String perfProfileName = KruizeOperator.setDefaultPerformanceProfile(kruizeObject.getSloInfo(), mode, target_cluster);
String perfProfileName;
if (!KruizeDeploymentInfo.local) {
perfProfileName = KruizeOperator.setDefaultPerformanceProfile(kruizeObject.getSloInfo(), mode, target_cluster);
} else {
perfProfileName = KruizeOperator.setDefaultMetricProfile(kruizeObject.getSloInfo(), mode, target_cluster);
}
kruizeObject.setPerformanceProfile(perfProfileName);
proceed = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@ public static final class PerformanceProfileConstants {
public static final String RESOURCE_OPT_OPENSHIFT_PROFILE = "resource-optimization-openshift";
public static final String RESOURCE_OPT_LOCAL_MON_PROFILE = "resource-optimization-local-monitoring";

//Metric profile constants
public static final String DEFAULT_API_VERSION = "recommender.com/v1";
public static final String DEFAULT_KIND = "KruizePerformanceProfile";

public static final Map<String, String> PerfProfileNames = Map.of(
RESOURCE_OPT_OPENSHIFT_PROFILE, "ResourceOptimizationOpenshiftImpl",
RESOURCE_OPT_LOCAL_MON_PROFILE, "ResourceOptimizationOpenshiftImpl"
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/autotune/database/dao/ExperimentDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public interface ExperimentDAO {
// Load a single Performance Profile based on name
List<KruizePerformanceProfileEntry> loadPerformanceProfileByName(String performanceProfileName) throws Exception;

// Load a single Metric Profile based on name
List<KruizeMetricProfileEntry> loadMetricProfileByName(String metricProfileName) throws Exception;

// Load all recommendations of a particular experiment and interval end Time
KruizeRecommendationEntry loadRecommendationsByExperimentNameAndDate(String experimentName, String cluster_name, Timestamp interval_end_time) throws Exception;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ public List<KruizePerformanceProfileEntry> loadPerformanceProfileByName(String p
}

/**
* Fetched Metric Profile by name from KruizeMetricProfileEntry database table
* Fetches Metric Profile by name from KruizeMetricProfileEntry database table
* @param metricProfileName Metric profile name
* @return List of KruizeMetricProfileEntry objects
* @throws Exception
Expand All @@ -869,7 +869,6 @@ public List<KruizeMetricProfileEntry> loadMetricProfileByName(String metricProfi
return entries;
}


@Override
public List<KruizeResultsEntry> getKruizeResultsEntry(String experiment_name, String cluster_name, Timestamp interval_start_time, Timestamp interval_end_time) throws Exception {
List<KruizeResultsEntry> kruizeResultsEntryList = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,27 @@ public void loadPerformanceProfileFromDBByName(Map<String, PerformanceProfile> p
}
}

/**
* Fetches Metric Profile by name from kruizeMetricProfileEntry
* @param metricProfileMap Map to store metric profile loaded from the database
* @param metricProfileName Metric profile name to be fetched
* @return ValidationOutputData object
*/
public void loadMetricProfileFromDBByName(Map<String, PerformanceProfile> metricProfileMap, String metricProfileName) throws Exception {
List<KruizeMetricProfileEntry> entries = experimentDAO.loadMetricProfileByName(metricProfileName);
if (null != entries && !entries.isEmpty()) {
List<PerformanceProfile> metricProfiles = DBHelpers.Converters.KruizeObjectConverters
.convertMetricProfileEntryToMetricProfileObject(entries);
if (!metricProfiles.isEmpty()) {
for (PerformanceProfile performanceProfile : metricProfiles) {
if (null != performanceProfile) {
PerformanceProfileUtil.addMetricProfile(metricProfileMap, performanceProfile);
}
}
}
}
}

public void loadAllExperimentsAndRecommendations(Map<String, KruizeObject> mainKruizeExperimentMap) throws Exception {

loadAllExperiments(mainKruizeExperimentMap);
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/com/autotune/operator/KruizeOperator.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import com.autotune.common.variables.Variables;
import com.autotune.utils.EventLogger;
import com.autotune.utils.KubeEventLogger;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.gson.Gson;
import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.ObjectMeta;
Expand Down Expand Up @@ -451,6 +453,39 @@ public static String setDefaultPerformanceProfile(SloInfo sloInfo, String mode,
return performanceProfile.getName();
}

public static String setDefaultMetricProfile(SloInfo sloInfo, String mode, String targetCluster) {
PerformanceProfile metricProfile = null;
try {
String apiVersion = AnalyzerConstants.PerformanceProfileConstants.DEFAULT_API_VERSION;
String kind = AnalyzerConstants.PerformanceProfileConstants.DEFAULT_KIND;
String name = AnalyzerConstants.PerformanceProfileConstants.DEFAULT_PROFILE;
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode metadataNode = objectMapper.createObjectNode();
metadataNode.put("name",name);

double profile_version = AnalyzerConstants.DEFAULT_PROFILE_VERSION;
String k8s_type = AnalyzerConstants.DEFAULT_K8S_TYPE;
metricProfile = new PerformanceProfile(apiVersion, kind, metadataNode, profile_version, k8s_type, sloInfo);

if (null != metricProfile) {
ValidationOutputData validationOutputData = PerformanceProfileUtil.validateAndAddMetricProfile(PerformanceProfilesDeployment.performanceProfilesMap, metricProfile);
if (validationOutputData.isSuccess()) {
LOGGER.info("Added metric Profile : {} into the map with version: {}",
metricProfile.getName(), metricProfile.getProfile_version());
} else {
new KubeEventLogger(Clock.systemUTC()).log("Failed", validationOutputData.getMessage(), EventLogger.Type.Warning, null, null, null, null);
}
} else {
new KubeEventLogger(Clock.systemUTC()).log("Failed", "Unable to create metric profile ", EventLogger.Type.Warning, null, null, null, null);
}
} catch (Exception e) {
LOGGER.error("Exception while adding Metric profile with message: {} ", e.getMessage());
new KubeEventLogger(Clock.systemUTC()).log("Failed", e.getMessage(), EventLogger.Type.Warning, null, null, null, null);
return null;
}
return metricProfile.getName();
}

/**
* Parse KruizeLayer JSON and create matching KruizeLayer object
*
Expand Down

0 comments on commit cc2da86

Please sign in to comment.