-
Notifications
You must be signed in to change notification settings - Fork 57
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
3. Add MetadataProfile database #1441
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
import com.autotune.database.init.KruizeHibernateUtil; | ||
import com.autotune.database.table.*; | ||
import com.autotune.database.table.lm.KruizeLMExperimentEntry; | ||
import com.autotune.database.table.lm.KruizeLMMetadataProfileEntry; | ||
import com.autotune.database.table.lm.KruizeLMRecommendationEntry; | ||
import com.autotune.utils.KruizeConstants; | ||
import com.autotune.utils.MetricsConfig; | ||
|
@@ -522,6 +523,44 @@ public ValidationOutputData addMetricProfileToDB(KruizeMetricProfileEntry kruize | |
return validationOutputData; | ||
} | ||
|
||
/** | ||
* Add MetadataProfile to database | ||
* | ||
* @param kruizeMetadataProfileEntry Metadata Profile Database object to be added | ||
* @return validationOutputData contains the status of the DB insert operation | ||
*/ | ||
@Override | ||
public ValidationOutputData addMetadataProfileToDB(KruizeLMMetadataProfileEntry kruizeMetadataProfileEntry) { | ||
ValidationOutputData validationOutputData = new ValidationOutputData(false, null, null); | ||
String statusValue = "failure"; | ||
Timer.Sample timerAddMetadataProfileDB = Timer.start(MetricsConfig.meterRegistry()); | ||
Transaction tx = null; | ||
try (Session session = KruizeHibernateUtil.getSessionFactory().openSession()) { | ||
try { | ||
tx = session.beginTransaction(); | ||
session.persist(kruizeMetadataProfileEntry); | ||
tx.commit(); | ||
validationOutputData.setSuccess(true); | ||
} catch (HibernateException e) { | ||
LOGGER.error("Not able to save metadata profile due to {}", e.getMessage()); | ||
if (tx != null) tx.rollback(); | ||
e.printStackTrace(); | ||
validationOutputData.setSuccess(false); | ||
validationOutputData.setMessage(e.getMessage()); | ||
} | ||
} catch (Exception e) { | ||
LOGGER.error("Not able to save metadata profile source due to {}", e.getMessage()); | ||
validationOutputData.setMessage(e.getMessage()); | ||
} finally { | ||
if (null != timerAddMetadataProfileDB) { | ||
MetricsConfig.timerAddMetadataProfileDB = MetricsConfig.timerBAddMetadataProfileDB.tag("status", statusValue).register(MetricsConfig.meterRegistry()); | ||
timerAddMetadataProfileDB.stop(MetricsConfig.timerAddMetadataProfileDB); | ||
} | ||
} | ||
|
||
return validationOutputData; | ||
} | ||
|
||
/** | ||
* @param kruizeDataSourceEntry | ||
* @param validationOutputData | ||
|
@@ -899,6 +938,32 @@ public List<KruizeMetricProfileEntry> loadAllMetricProfiles() throws Exception { | |
return entries; | ||
} | ||
|
||
/** | ||
* Fetches all the Metadata Profile records from KruizeLMMetadataProfileEntry database table | ||
* | ||
* @return List of all KruizeLMMetadataProfileEntry database objects | ||
* @throws Exception | ||
*/ | ||
@Override | ||
public List<KruizeLMMetadataProfileEntry> loadAllMetadataProfiles() throws Exception { | ||
String statusValue = "failure"; | ||
Timer.Sample timerLoadAllMetadataProfiles = Timer.start(MetricsConfig.meterRegistry()); | ||
|
||
List<KruizeLMMetadataProfileEntry> entries = null; | ||
try (Session session = KruizeHibernateUtil.getSessionFactory().openSession()) { | ||
entries = session.createQuery(DBConstants.SQLQUERY.SELECT_FROM_METADATA_PROFILE, KruizeLMMetadataProfileEntry.class).list(); | ||
} catch (Exception e) { | ||
LOGGER.error("Not able to load Metadata Profile due to {}", e.getMessage()); | ||
throw new Exception("Error while loading existing Metadata Profile from database due to : " + e.getMessage()); | ||
} finally { | ||
if (null != timerLoadAllMetadataProfiles) { | ||
MetricsConfig.timerLoadAllMetadataProfiles = MetricsConfig.timerBLoadAllMetadataProfiles.tag("status", statusValue).register(MetricsConfig.meterRegistry()); | ||
timerLoadAllMetadataProfiles.stop(MetricsConfig.timerLoadAllMetadataProfiles); | ||
} | ||
} | ||
return entries; | ||
} | ||
|
||
@Override | ||
public List<KruizeLMExperimentEntry> loadLMExperimentByName(String experimentName) throws Exception { | ||
//todo load only experimentStatus=inprogress , playback may not require completed experiments | ||
|
@@ -1209,6 +1274,32 @@ public List<KruizeMetricProfileEntry> loadMetricProfileByName(String metricProfi | |
return entries; | ||
} | ||
|
||
/** | ||
* Fetches Metadata Profile by name from KruizeLMMetadataProfileEntry database table | ||
* | ||
* @param metadataProfileName Metadata profile name | ||
* @return List of KruizeLMMetadataProfileEntry objects | ||
* @throws Exception | ||
*/ | ||
public List<KruizeLMMetadataProfileEntry> loadMetadataProfileByName(String metadataProfileName) throws Exception { | ||
String statusValue = "failure"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @shreyabiradar07 In other methods there is a finally block too that uses the statusValue, I think it is required here too. Can you check and add it.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have included the finally blocks with the new timer metrics added. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @shreyabiradar07 Have you tested the timer metrics? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When I try to query one of the above DB methods -
The database methods added as part of this PR are currently not being invoked. These methods added will be used in the subsequent PRs with MetadataProfile REST APIs. |
||
Timer.Sample timerLoadMetadataProfileName = Timer.start(MetricsConfig.meterRegistry()); | ||
List<KruizeLMMetadataProfileEntry> entries = null; | ||
try (Session session = KruizeHibernateUtil.getSessionFactory().openSession()) { | ||
entries = session.createQuery(DBConstants.SQLQUERY.SELECT_FROM_METADATA_PROFILE_BY_NAME, KruizeLMMetadataProfileEntry.class) | ||
.setParameter("name", metadataProfileName).list(); | ||
} catch (Exception e) { | ||
LOGGER.error("Not able to load Metadata Profile {} due to {}", metadataProfileName, e.getMessage()); | ||
throw new Exception("Error while loading existing metadata profile from database due to : " + e.getMessage()); | ||
} finally { | ||
if (null != timerLoadMetadataProfileName) { | ||
MetricsConfig.timerLoadMetadataProfileName = MetricsConfig.timerBLoadMetadataProfileName.tag("status", statusValue).register(MetricsConfig.meterRegistry()); | ||
timerLoadMetadataProfileName.stop(MetricsConfig.timerLoadMetadataProfileName); | ||
} | ||
} | ||
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<>(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Red Hat, IBM Corporation and others. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*******************************************************************************/ | ||
package com.autotune.database.table.lm; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import jakarta.persistence.*; | ||
import org.hibernate.annotations.JdbcTypeCode; | ||
import org.hibernate.type.SqlTypes; | ||
|
||
/** | ||
* This is a Java class named KruizeLMMetadataProfileEntry annotated with JPA annotations. | ||
* It represents a table named kruize_lm_metadata_profiles in a relational database. | ||
* <p> | ||
* The class has the following fields: | ||
* <p> | ||
* id: A unique identifier for each metadata profile detail. | ||
* apiVersion: A string representing version of the Kubernetes API to create this object | ||
* kind: A string representing type of kubernetes object | ||
* metadata: A JSON object containing the metadata of the CRD, including name field | ||
* name: A string representing the name of the metadata profile. | ||
* profile_version: A string representing the version of the metadata profile. | ||
* k8s_type: A string representing kubernetes type. | ||
* query_variables: A JSON object containing metadata queries | ||
*/ | ||
@Entity | ||
@Table(name = "kruize_lm_metadata_profiles") | ||
public class KruizeLMMetadataProfileEntry { | ||
private String api_version; | ||
private String kind; | ||
@JdbcTypeCode(SqlTypes.JSON) | ||
private JsonNode metadata; | ||
@Id | ||
private String name; | ||
private double profile_version; | ||
private String k8s_type; | ||
@JdbcTypeCode(SqlTypes.JSON) | ||
private JsonNode query_variables; | ||
|
||
public String getApi_version() { | ||
return api_version; | ||
} | ||
|
||
public void setApi_version(String api_version) { | ||
this.api_version = api_version; | ||
} | ||
|
||
public String getKind() { | ||
return kind; | ||
} | ||
|
||
public void setKind(String kind) { | ||
this.kind = kind; | ||
} | ||
|
||
public JsonNode getMetadata() { | ||
return metadata; | ||
} | ||
|
||
public void setMetadata(JsonNode metadata) { | ||
this.metadata = metadata; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public double getProfile_version() { | ||
return profile_version; | ||
} | ||
|
||
public void setProfile_version(double profile_version) { | ||
this.profile_version = profile_version; | ||
} | ||
|
||
public String getK8s_type() { | ||
return k8s_type; | ||
} | ||
|
||
public void setK8s_type(String k8s_type) { | ||
this.k8s_type = k8s_type; | ||
} | ||
|
||
public JsonNode getQuery_variables() { | ||
return query_variables; | ||
} | ||
|
||
public void setQuery_variables(JsonNode query_variables) { | ||
this.query_variables = query_variables; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shreyabiradar07 where is statusValue being used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing this out, I have added timer metrics which use the
statusValue
to track metrics for each database operation.