Skip to content

Commit

Permalink
3-Migration CreateExperiment API rm and lm support contd..
Browse files Browse the repository at this point in the history
Signed-off-by: msvinaykumar <[email protected]>
  • Loading branch information
msvinaykumar committed Dec 6, 2024
1 parent 3d7b84d commit e1de006
Show file tree
Hide file tree
Showing 21 changed files with 230 additions and 103 deletions.
4 changes: 2 additions & 2 deletions migrations/kruize_local_ddl.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
create table IF NOT EXISTS kruize_lm_experiments (experiment_id varchar(255) not null, cluster_name varchar(255), datasource jsonb, experiment_name varchar(255), extended_data jsonb, meta_data jsonb, mode varchar(255), performance_profile varchar(255), status varchar(255), target_cluster varchar(255), version varchar(255), primary key (experiment_id));
create table IF NOT EXISTS kruize_lm_experiments (experiment_id varchar(255) not null, cluster_name varchar(255), experiment_name varchar(255), extended_data jsonb, meta_data jsonb, mode varchar(255), performance_profile varchar(255), status varchar(255), target_cluster varchar(255), version varchar(255),experiment_type varchar(255),datasource varchar(255),creation_date timestamp(6) ,updated_date timestamp(6) , primary key (experiment_id));
create table IF NOT EXISTS kruize_authentication (id serial, authentication_type varchar(255), credentials jsonb, service_type varchar(255), primary key (id));
create table IF NOT EXISTS kruize_datasources (version varchar(255), name varchar(255), provider varchar(255), serviceName varchar(255), namespace varchar(255), url varchar(255), authentication_id serial, FOREIGN KEY (authentication_id) REFERENCES kruize_authentication(id), primary key (name));
create table IF NOT EXISTS kruize_dsmetadata (id serial, version varchar(255), datasource_name varchar(255), cluster_name varchar(255), namespace varchar(255), workload_type varchar(255), workload_name varchar(255), container_name varchar(255), container_image_name varchar(255), primary key (id));
alter table kruize_lm_experiments add column experiment_type varchar(255), add column metadata_id bigint references kruize_dsmetadata(id), alter column datasource type varchar(255);
alter table kruize_lm_experiments add column metadata_id bigint references kruize_dsmetadata(id);
alter table if exists kruize_lm_experiments add constraint UK_lm_experiment_name unique (experiment_name);
create table IF NOT EXISTS kruize_metric_profiles (api_version varchar(255), kind varchar(255), metadata jsonb, name varchar(255) not null, k8s_type varchar(255), profile_version float(53) not null, slo jsonb, primary key (name));
alter table kruize_recommendations add column experiment_type varchar(255);
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ public void validate(List<KruizeObject> kruizeExptList) {
if (validationOutputData.isSuccess()) {
String expName = kruizeObject.getExperimentName();
try {
new ExperimentDBService().loadExperimentFromDBByName(mainKruizeExperimentMAP, expName);
if (KruizeDeploymentInfo.is_ros_enabled && kruizeObject.getTarget_cluster().equalsIgnoreCase(AnalyzerConstants.REMOTE)) { // todo call this in function and use across every where
new ExperimentDBService().loadExperimentFromDBByName(mainKruizeExperimentMAP, expName);
} else {
new ExperimentDBService().loadLMExperimentFromDBByName(mainKruizeExperimentMAP, expName);
}
} catch (Exception e) {
LOGGER.error("Loading saved experiment {} failed: {} ", expName, e.getMessage());
}
Expand Down
30 changes: 15 additions & 15 deletions src/main/java/com/autotune/analyzer/kruizeObject/KruizeObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public final class KruizeObject implements ExperimentTypeAware {
@SerializedName("datasource")
private String datasource;
@SerializedName(KruizeConstants.JSONKeys.EXPERIMENT_TYPE) //TODO: to be used in future
private String experimentType;
private AnalyzerConstants.ExperimentType experimentType;
private String namespace; // TODO: Currently adding it at this level with an assumption that there is only one entry in k8s object needs to be changed
private String mode; //Todo convert into Enum
@SerializedName("target_cluster")
Expand Down Expand Up @@ -122,10 +122,10 @@ public KruizeObject() {
* Sets default terms for a KruizeObject.
* This method initializes a map with predefined terms like "SHORT_TERM", "MEDIUM_TERM", and "LONG_TERM".
* Each term is defined by a Terms object containing: Name of the term (e.g., "SHORT_TERM"), Duration (in days) to
be considered under that term, Threshold for the duration.
be considered under that term, Threshold for the duration.
* Note: Currently, specific term names like "daily", "weekly", and "fortnightly" are not defined.
* This method also requires implementing CustomResourceDefinition yaml for managing terms. This
functionality is not currently included.
functionality is not currently included.
@param terms A map to store the default terms with term name as the key and Terms object as the value.
@param kruizeObject The KruizeObject for which the default terms are being set.
*/
Expand Down Expand Up @@ -301,24 +301,14 @@ public void setDataSource(String datasource) {
this.datasource = datasource;
}

@Override
public String getExperimentType() {
public AnalyzerConstants.ExperimentType getExperimentType() {
return experimentType;
}

public void setExperimentType(String experimentType) {
public void setExperimentType(AnalyzerConstants.ExperimentType experimentType) {
this.experimentType = experimentType;
}

@Override
public boolean isNamespaceExperiment() {
return ExperimentTypeUtil.isNamespaceExperiment(experimentType);
}

@Override
public boolean isContainerExperiment() {
return ExperimentTypeUtil.isContainerExperiment(experimentType);
}

@Override
public String toString() {
Expand Down Expand Up @@ -347,4 +337,14 @@ public String toString() {
", kubernetes_objects=" + kubernetes_objects +
'}';
}

@Override
public boolean isNamespaceExperiment() {
return ExperimentTypeUtil.isNamespaceExperiment(experimentType);
}

@Override
public boolean isContainerExperiment() {
return ExperimentTypeUtil.isContainerExperiment(experimentType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,17 @@ public void setKruizeObject(KruizeObject kruizeObject) {
this.kruizeObject = kruizeObject;
}

private KruizeObject createKruizeObject() {
private KruizeObject createKruizeObject(String target_cluster) {
Map<String, KruizeObject> mainKruizeExperimentMAP = new ConcurrentHashMap<>();
KruizeObject kruizeObject = new KruizeObject();
try {
new ExperimentDBService().loadExperimentFromDBByName(mainKruizeExperimentMAP, experimentName);

if (KruizeDeploymentInfo.is_ros_enabled && null != target_cluster && target_cluster.equalsIgnoreCase(AnalyzerConstants.REMOTE)) { // todo call this in function and use across every where
new ExperimentDBService().loadExperimentFromDBByName(mainKruizeExperimentMAP, experimentName);
} else {
new ExperimentDBService().loadLMExperimentFromDBByName(mainKruizeExperimentMAP, experimentName);
}

if (null != mainKruizeExperimentMAP.get(experimentName)) {
kruizeObject = mainKruizeExperimentMAP.get(experimentName);
kruizeObject.setValidation_data(new ValidationOutputData(true, null, null));
Expand Down Expand Up @@ -259,7 +265,7 @@ public String validate_local() { //TODO Instead of relying on the 'lo
* @param calCount The count of incoming requests.
* @return The KruizeObject containing the prepared recommendations.
*/
public KruizeObject prepareRecommendations(int calCount) throws FetchMetricsError {
public KruizeObject prepareRecommendations(int calCount, String target_cluster) throws FetchMetricsError {
Map<String, KruizeObject> mainKruizeExperimentMAP = new ConcurrentHashMap<>();
Map<String, Terms> terms = new HashMap<>();
ValidationOutputData validationOutputData;
Expand All @@ -269,7 +275,7 @@ public KruizeObject prepareRecommendations(int calCount) throws FetchMetricsErro
intervalEndTimeStr);
setInterval_end_time(interval_end_time);
}
KruizeObject kruizeObject = createKruizeObject();
KruizeObject kruizeObject = createKruizeObject(target_cluster);
if (!kruizeObject.getValidation_data().isSuccess())
return kruizeObject;
setKruizeObject(kruizeObject);
Expand Down Expand Up @@ -2388,7 +2394,7 @@ private void prepareIntervalResults(Map<Timestamp, IntervalResults> dataResultsM
* @param maxDateQuery maxDateQuery metric to be filtered out
* @param experimentType experiment type
*/
public List<Metric> filterMetricsBasedOnExpTypeAndK8sObject(PerformanceProfile metricProfile, String maxDateQuery, String experimentType) {
public List<Metric> filterMetricsBasedOnExpTypeAndK8sObject(PerformanceProfile metricProfile, String maxDateQuery, AnalyzerConstants.ExperimentType experimentType) {
String namespace = KruizeConstants.JSONKeys.NAMESPACE;
String container = KruizeConstants.JSONKeys.CONTAINER;
return metricProfile.getSloInfo().getFunctionVariables().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import com.autotune.analyzer.recommendations.NamespaceRecommendations;
import com.autotune.analyzer.recommendations.objects.MappedRecommendationForTimestamp;
import com.autotune.analyzer.utils.AnalyzerConstants;
import com.autotune.analyzer.utils.AnalyzerErrorConstants;
import com.autotune.analyzer.utils.ExperimentTypeUtil;
import com.autotune.common.data.ValidationOutputData;
import com.autotune.common.data.metrics.AggregationFunctions;
import com.autotune.common.data.metrics.Metric;
Expand All @@ -23,6 +21,8 @@
import com.autotune.common.k8sObjects.K8sObject;
import com.autotune.utils.KruizeConstants;
import com.autotune.utils.Utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.gson.Gson;
import org.json.JSONArray;
import org.json.JSONObject;
Expand All @@ -31,12 +31,9 @@

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class Converters {
private Converters() {
Expand Down Expand Up @@ -70,7 +67,7 @@ public static KruizeObject convertCreateExperimentAPIObjToKruizeObject(CreateExp
// namespace recommendations experiment type
k8sObject = createNamespaceExperiment(kubernetesAPIObject);
}
LOGGER.debug("Experiment Type: " + createExperimentAPIObject.getExperimentType());
LOGGER.debug("Experiment Type: {}", createExperimentAPIObject.getExperimentType());
k8sObjectList.add(k8sObject);
}
kruizeObject.setKubernetes_objects(k8sObjectList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class CreateExperimentAPIObject extends BaseSO implements ExperimentTypeA
@SerializedName(KruizeConstants.JSONKeys.DATASOURCE) //TODO: to be used in future
private String datasource;
@SerializedName(KruizeConstants.JSONKeys.EXPERIMENT_TYPE) //TODO: to be used in future
private String experimentType;
private AnalyzerConstants.ExperimentType experimentType;
private AnalyzerConstants.ExperimentStatus status;
private String experiment_id; // this id is UUID and getting set at createExperiment API
private ValidationOutputData validationData; // This object indicates if this API object is valid or invalid
Expand Down Expand Up @@ -151,25 +151,14 @@ public void setDatasource(String datasource) {
this.datasource = datasource;
}

@Override
public String getExperimentType() {
public AnalyzerConstants.ExperimentType getExperimentType() {
return experimentType;
}

public void setExperimentType(String experimentType) {
public void setExperimentType(AnalyzerConstants.ExperimentType experimentType) {
this.experimentType = experimentType;
}

@Override
public boolean isNamespaceExperiment() {
return ExperimentTypeUtil.isNamespaceExperiment(experimentType);
}

@Override
public boolean isContainerExperiment() {
return ExperimentTypeUtil.isContainerExperiment(experimentType);
}

@Override
public String toString() {
return "CreateExperimentAPIObject{" +
Expand All @@ -186,5 +175,15 @@ public String toString() {
", recommendationSettings=" + recommendationSettings +
'}';
}

@Override
public boolean isNamespaceExperiment() {
return ExperimentTypeUtil.isNamespaceExperiment(experimentType);
}

@Override
public boolean isContainerExperiment() {
return ExperimentTypeUtil.isContainerExperiment(experimentType);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*******************************************************************************/
package com.autotune.analyzer.serviceObjects;

import com.autotune.analyzer.utils.AnalyzerConstants;
import com.autotune.analyzer.utils.ExperimentTypeAware;
import com.autotune.analyzer.utils.ExperimentTypeUtil;
import com.autotune.utils.KruizeConstants;
Expand All @@ -26,7 +27,7 @@ public class ListRecommendationsAPIObject extends BaseSO implements ExperimentTy
@SerializedName(KruizeConstants.JSONKeys.CLUSTER_NAME)
private String clusterName;
@SerializedName(KruizeConstants.JSONKeys.EXPERIMENT_TYPE)
private String experimentType;
private AnalyzerConstants.ExperimentType experimentType;

@SerializedName(KruizeConstants.JSONKeys.KUBERNETES_OBJECTS)
private List<KubernetesAPIObject> kubernetesObjects;
Expand All @@ -47,12 +48,11 @@ public void setKubernetesObjects(List<KubernetesAPIObject> kubernetesObjects) {
this.kubernetesObjects = kubernetesObjects;
}

@Override
public String getExperimentType() {
public AnalyzerConstants.ExperimentType getExperimentType() {
return experimentType;
}

public void setExperimentType(String experimentType) {
public void setExperimentType(AnalyzerConstants.ExperimentType experimentType) {
this.experimentType = experimentType;
}

Expand Down
21 changes: 9 additions & 12 deletions src/main/java/com/autotune/analyzer/services/CreateExperiment.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,14 @@

package com.autotune.analyzer.services;

import com.autotune.analyzer.exceptions.InvalidExperimentType;
import com.autotune.analyzer.exceptions.KruizeResponse;
import com.autotune.analyzer.experiment.ExperimentInitiator;
import com.autotune.analyzer.kruizeObject.KruizeObject;
import com.autotune.analyzer.serviceObjects.Converters;
import com.autotune.analyzer.serviceObjects.CreateExperimentAPIObject;
import com.autotune.analyzer.serviceObjects.KubernetesAPIObject;
import com.autotune.analyzer.utils.AnalyzerConstants;
import com.autotune.analyzer.utils.AnalyzerErrorConstants;
import com.autotune.common.data.ValidationOutputData;
import com.autotune.common.data.result.ContainerData;
import com.autotune.common.data.result.NamespaceData;
import com.autotune.common.k8sObjects.K8sObject;
import com.autotune.database.dao.ExperimentDAO;
import com.autotune.database.dao.ExperimentDAOImpl;
import com.autotune.database.service.ExperimentDBService;
Expand All @@ -47,7 +42,10 @@
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -101,9 +99,9 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
createExperimentAPIObject.setExperiment_id(Utils.generateID(createExperimentAPIObject.toString()));
createExperimentAPIObject.setStatus(AnalyzerConstants.ExperimentStatus.IN_PROGRESS);
// validating the kubernetes objects and experiment type
for (KubernetesAPIObject kubernetesAPIObject: createExperimentAPIObject.getKubernetesObjects()) {
/*for (KubernetesAPIObject kubernetesAPIObject : createExperimentAPIObject.getKubernetesObjects()) {
if (createExperimentAPIObject.isContainerExperiment()) {
createExperimentAPIObject.setExperimentType(AnalyzerConstants.ExperimentTypes.CONTAINER_EXPERIMENT);
createExperimentAPIObject.setExperimentType(AnalyzerConstants.ExperimentType.CONTAINER);
// check if namespace data is also set for container-type experiments
if (null != kubernetesAPIObject.getNamespaceAPIObjects()) {
throw new InvalidExperimentType(AnalyzerErrorConstants.APIErrors.CreateExperimentAPI.NAMESPACE_DATA_NOT_NULL_FOR_CONTAINER_EXP);
Expand All @@ -116,7 +114,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
throw new InvalidExperimentType(AnalyzerErrorConstants.APIErrors.CreateExperimentAPI.NAMESPACE_EXP_NOT_SUPPORTED_FOR_REMOTE);
}
}
}
}*/
KruizeObject kruizeObject = Converters.KruizeObjectConverters.convertCreateExperimentAPIObjToKruizeObject(createExperimentAPIObject);
if (null != kruizeObject)
kruizeExpList.add(kruizeObject);
Expand Down Expand Up @@ -149,9 +147,8 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
e.printStackTrace();
LOGGER.error("Unknown exception caught: " + e.getMessage());
sendErrorResponse(inputData, response, e, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Internal Server Error: " + e.getMessage());
} catch (InvalidExperimentType e) {
sendErrorResponse(inputData, response, null, HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
} finally {
} //catch (InvalidExperimentType e) { sendErrorResponse(inputData, response, null, HttpServletResponse.SC_BAD_REQUEST, e.getMessage()); }
finally {
if (null != timerCreateExp) {
MetricsConfig.timerCreateExp = MetricsConfig.timerBCreateExp.tag("status", statusValue).register(MetricsConfig.meterRegistry());
timerCreateExp.stop(MetricsConfig.timerCreateExp);
Expand Down
Loading

0 comments on commit e1de006

Please sign in to comment.