Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4. Add MetadataProfile Create and List APIs #1442

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion migrations/kruize_local_ddl.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ alter table kruize_lm_experiments add column metadata_id bigint references krui
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));
create table IF NOT EXISTS kruize_lm_recommendations (interval_end_time timestamp(6) not null, experiment_name varchar(255) not null, cluster_name varchar(255), extended_data jsonb, version varchar(255),experiment_type varchar(255), primary key (experiment_name, interval_end_time)) PARTITION BY RANGE (interval_end_time);
create table IF NOT EXISTS kruize_lm_metadata_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, query_variables jsonb, primary key (name));
create table IF NOT EXISTS kruize_lm_metadata_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, datasource varchar(255) not null, query_variables jsonb, primary key (name));
2 changes: 2 additions & 0 deletions src/main/java/com/autotune/analyzer/Analyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public static void addServlets(ServletContextHandler context) {
context.addServlet(MetricProfileService.class, ServerContext.CREATE_METRIC_PROFILE);
context.addServlet(MetricProfileService.class, ServerContext.LIST_METRIC_PROFILES);
context.addServlet(MetricProfileService.class, ServerContext.DELETE_METRIC_PROFILE);
context.addServlet(MetadataProfileService.class, ServerContext.CREATE_METADATA_PROFILE);
context.addServlet(MetadataProfileService.class, ServerContext.LIST_METADATA_PROFILES);
context.addServlet(ListDatasources.class, ServerContext.LIST_DATASOURCES);
context.addServlet(DSMetadataService.class, ServerContext.DATASOURCE_METADATA);
context.addServlet(BulkService.class, ServerContext.BULK_SERVICE);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.autotune.analyzer.exceptions;

public class MetadataProfileResponse {
private String message;
private int httpcode;
private String documentationLink;
private String status;

public MetadataProfileResponse(String message, int httpcode, String documentationLink, String status) {
this.message = message;
this.httpcode = httpcode;
this.documentationLink = documentationLink;
this.status = status;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public int getHttpcode() {
return httpcode;
}

public void setHttpcode(int httpcode) {
this.httpcode = httpcode;
}

public String getDocumentationLink() {
return documentationLink;
}

public void setDocumentationLink(String documentationLink) {
this.documentationLink = documentationLink;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,21 @@ public class MetadataProfile {
@SerializedName("k8s_type")
private String k8s_type;

private String datasource;

@SerializedName("query_variables")
private ArrayList<Metric> metrics;

public MetadataProfile(String apiVersion, String kind, JsonNode metadata,
double profile_version, String k8s_type, ArrayList<Metric> metrics) {
double profile_version, String k8s_type, String datasource, ArrayList<Metric> metrics) {

this.apiVersion = apiVersion;
this.kind = kind;
this.metadata = metadata;
this.name = metadata.get("name").asText();
this.profile_version = profile_version;
this.k8s_type = k8s_type;
this.datasource = datasource;
this.metrics = metrics;
}

Expand Down Expand Up @@ -83,6 +86,8 @@ public String getK8s_type() {
return k8s_type;
}

public String getDatasource() { return datasource; }

public ArrayList<Metric> getQueryVariables() {
return new ArrayList<>(metrics);
}
Expand All @@ -96,6 +101,7 @@ public String toString() {
", name='" + name + '\'' +
", profile_version=" + profile_version +
", k8s_type='" + k8s_type + '\'' +
", datasource='" + datasource + '\'' +
", query_variables=" + metrics +
'}';
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.autotune.analyzer.metadataProfiles;

import com.autotune.database.service.ExperimentDBService;
import com.autotune.utils.KruizeConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class MetadataProfileCollection {
private static final Logger LOGGER = LoggerFactory.getLogger(MetadataProfileCollection.class);
private static MetadataProfileCollection metadataProfileCollectionInstance = new MetadataProfileCollection();
private HashMap<String, MetadataProfile> metadataProfileCollection;

private MetadataProfileCollection() {
this.metadataProfileCollection = new HashMap<>();
}

public static MetadataProfileCollection getInstance() {
return metadataProfileCollectionInstance;
}

public HashMap<String, MetadataProfile> getMetadataProfileCollection() {
return metadataProfileCollection;
}

public void loadMetadataProfilesFromDB() {
try {
LOGGER.info(KruizeConstants.MetadataProfileConstants.CHECKING_AVAILABLE_METADATA_PROFILE_FROM_DB);
Map<String, MetadataProfile> availableMetadataProfiles = new HashMap<>();
new ExperimentDBService().loadAllMetadataProfiles(availableMetadataProfiles);
if (availableMetadataProfiles.isEmpty()) {
LOGGER.info(KruizeConstants.MetadataProfileConstants.NO_METADATA_PROFILE_FOUND_IN_DB);
}else {
for (Map.Entry<String, MetadataProfile> metadataProfile : availableMetadataProfiles.entrySet()) {
LOGGER.info(KruizeConstants.MetadataProfileConstants.METADATA_PROFILE_FOUND, metadataProfile.getKey());
metadataProfileCollection.put(metadataProfile.getKey(), metadataProfile.getValue());
}
}

} catch (Exception e) {
LOGGER.error(e.getMessage());
}
}


public void addMetadataProfile(MetadataProfile metadataProfile) {
String metadataProfileName = metadataProfile.getMetadata().get("name").asText();

LOGGER.info(KruizeConstants.MetadataProfileConstants.ADDING_METADATA_PROFILE + "{}", metadataProfileName);

if(metadataProfileCollection.containsKey(metadataProfileName)) {
LOGGER.error(KruizeConstants.MetadataProfileConstants.METADATA_PROFILE_ALREADY_EXISTS + "{}", metadataProfileName);
} else {
LOGGER.info(KruizeConstants.MetadataProfileConstants.METADATA_PROFILE_ADDED + "{}", metadataProfileName);
metadataProfileCollection.put(metadataProfileName, metadataProfile);
}
}
}
46 changes: 46 additions & 0 deletions src/main/java/com/autotune/analyzer/serviceObjects/Converters.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.autotune.analyzer.kruizeObject.KruizeObject;
import com.autotune.analyzer.kruizeObject.ObjectiveFunction;
import com.autotune.analyzer.kruizeObject.SloInfo;
import com.autotune.analyzer.metadataProfiles.MetadataProfile;
import com.autotune.analyzer.performanceProfiles.PerformanceProfile;
import com.autotune.analyzer.recommendations.ContainerRecommendations;
import com.autotune.analyzer.recommendations.NamespaceRecommendations;
Expand Down Expand Up @@ -388,6 +389,51 @@ public static PerformanceProfile convertInputJSONToCreateMetricProfile(String in
return metricProfile;
}

public static MetadataProfile convertInputJSONToCreateMetadataProfile(String inputData) throws InvalidValueException, Exception {
MetadataProfile metadataProfile = null;
if (inputData != null) {
JSONObject jsonObject = new JSONObject(inputData);
String apiVersion = jsonObject.getString(AnalyzerConstants.API_VERSION);
String kind = jsonObject.getString(AnalyzerConstants.KIND);

JSONObject metadataObject = jsonObject.getJSONObject(AnalyzerConstants.AutotuneObjectConstants.METADATA);
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode metadata = objectMapper.readValue(metadataObject.toString(), ObjectNode.class);
metadata.put("name", metadataObject.getString("name"));

Double profileVersion = jsonObject.has(AnalyzerConstants.PROFILE_VERSION) ? jsonObject.getDouble(AnalyzerConstants.PROFILE_VERSION) : null;
String k8sType = jsonObject.has(AnalyzerConstants.MetadataProfileConstants.K8S_TYPE) ? jsonObject.getString(AnalyzerConstants.MetadataProfileConstants.K8S_TYPE) : null;
String datasource = jsonObject.has(AnalyzerConstants.MetadataProfileConstants.DATASOURCE) ? jsonObject.getString(AnalyzerConstants.MetadataProfileConstants.DATASOURCE) : null;
JSONArray queryVariableArray = jsonObject.getJSONArray(AnalyzerConstants.AutotuneObjectConstants.QUERY_VARIABLES);
ArrayList<Metric> queryVariablesList = new ArrayList<>();
for (Object object : queryVariableArray) {
JSONObject functionVarObj = (JSONObject) object;
String name = functionVarObj.getString(AnalyzerConstants.AutotuneObjectConstants.NAME);
datasource = functionVarObj.has(AnalyzerConstants.AutotuneObjectConstants.DATASOURCE) ? functionVarObj.getString(AnalyzerConstants.AutotuneObjectConstants.DATASOURCE) : datasource;
String query = functionVarObj.has(AnalyzerConstants.AutotuneObjectConstants.QUERY) ? functionVarObj.getString(AnalyzerConstants.AutotuneObjectConstants.QUERY) : null;
String valueType = functionVarObj.getString(AnalyzerConstants.AutotuneObjectConstants.VALUE_TYPE);
String kubeObject = functionVarObj.has(AnalyzerConstants.KUBERNETES_OBJECT) ? functionVarObj.getString(AnalyzerConstants.KUBERNETES_OBJECT) : null;
Metric metric = new Metric(name, query, datasource, valueType, kubeObject);
JSONArray aggrFunctionArray = functionVarObj.has(AnalyzerConstants.AGGREGATION_FUNCTIONS) ? functionVarObj.getJSONArray(AnalyzerConstants.AGGREGATION_FUNCTIONS) : null;
HashMap<String, AggregationFunctions> aggregationFunctionsMap = new HashMap<>();
for (Object innerObject : aggrFunctionArray) {
JSONObject aggrFuncJsonObject = (JSONObject) innerObject;
String function = aggrFuncJsonObject.getString(AnalyzerConstants.FUNCTION);
String aggrFuncQuery = aggrFuncJsonObject.getString(KruizeConstants.JSONKeys.QUERY);
String version = aggrFuncJsonObject.has(KruizeConstants.JSONKeys.VERSION) ? aggrFuncJsonObject.getString(KruizeConstants.JSONKeys.VERSION) : null;
AggregationFunctions aggregationFunctions = new AggregationFunctions(function, aggrFuncQuery, version);
aggregationFunctionsMap.put(function, aggregationFunctions);
}
metric.setAggregationFunctionsMap(aggregationFunctionsMap);
queryVariablesList.add(metric);
}

metadataProfile = new MetadataProfile(apiVersion, kind, metadata, profileVersion, k8sType, datasource, queryVariablesList);
}
return metadataProfile;
}


public static ConcurrentHashMap<String, KruizeObject> ConvertUpdateResultDataToAPIResponse(ConcurrentHashMap<String, KruizeObject> mainKruizeExperimentMap) {
return null;
}
Expand Down
Loading