Skip to content

Commit

Permalink
resolve code conflicts by loading results by clusterName
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyabiradar07 committed Sep 15, 2023
1 parent 2fa6a46 commit 2a4d524
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,47 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
} catch (Exception e) {
LOGGER.error("Loading saved cluster {} failed: {} ", clusterName, e.getMessage());
}
// Check if cluster exists
if (mKruizeExperimentMap.containsKey(clusterName)) {
// Check if timestamp is passed
if (null != monitoringEndTime && !monitoringEndTime.isEmpty()) {
monitoringEndTime = monitoringEndTime.trim();
if (Utils.DateUtils.isAValidDate(KruizeConstants.DateFormats.STANDARD_JSON_DATE_FORMAT, monitoringEndTime)) {
Date mEndTime = Utils.DateUtils.getDateFrom(KruizeConstants.DateFormats.STANDARD_JSON_DATE_FORMAT, monitoringEndTime);
monitoringEndTimestamp = new Timestamp(mEndTime.getTime());
// Check if timestamp exists in recommendations
boolean timestampExists = ServiceHelpers.KruizeObjectOperations.checkRecommendationTimestampExists(mKruizeExperimentMap.get(clusterName), monitoringEndTime);
if (timestampExists) {
checkForTimestamp = true;
} else {
error = true;
sendErrorResponse(
response,
new Exception(AnalyzerErrorConstants.APIErrors.ListRecommendationsAPI.RECOMMENDATION_DOES_NOT_EXIST_EXCPTN),
HttpServletResponse.SC_BAD_REQUEST,
String.format(AnalyzerErrorConstants.APIErrors.ListRecommendationsAPI.RECOMMENDATION_DOES_NOT_EXIST_MSG, monitoringEndTime)
);
}
} else {
error = true;
sendErrorResponse(
response,
new Exception(AnalyzerErrorConstants.APIErrors.ListRecommendationsAPI.INVALID_TIMESTAMP_EXCPTN),
HttpServletResponse.SC_BAD_REQUEST,
String.format(AnalyzerErrorConstants.APIErrors.ListRecommendationsAPI.INVALID_TIMESTAMP_MSG, monitoringEndTime)
);
}
}
kruizeObjectList.add(mKruizeExperimentMap.get(clusterName));
} else {
error = true;
sendErrorResponse(
response,
new Exception(AnalyzerErrorConstants.APIErrors.ListRecommendationsAPI.INVALID_CLUSTER_NAME_EXCPTN),
HttpServletResponse.SC_BAD_REQUEST,
String.format(AnalyzerErrorConstants.APIErrors.ListRecommendationsAPI.INVALID_CLUSTER_NAME_MSG, clusterName)
);
}
}else {
try {
new ExperimentDBService().loadAllExperimentsAndRecommendations(mKruizeExperimentMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ public static final class ListRecommendationsAPI {
public static final String INVALID_EXPERIMENT_NAME_MSG = "Given experiment name - \" %s \" is not valid";
public static final String INVALID_QUERY_PARAM = "The query param(s) - \" %s \" is/are invalid";
public static final String INVALID_QUERY_PARAM_VALUE = "The query param value(s) is/are invalid";
public static final String INVALID_CLUSTER_NAME_EXCPTN = "Invalid Cluster Name";
public static final String INVALID_CLUSTER_NAME_MSG = "Given cluster name - \" %s \" is not valid";

private ListRecommendationsAPI() {

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/autotune/database/dao/ExperimentDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public interface ExperimentDAO {
// Load all results for a particular experimentName
List<KruizeResultsEntry> loadResultsByExperimentName(String experimentName, Timestamp interval_start_time, Integer limitRows) throws Exception;

// Load all results for a particular clusterName
List<KruizeResultsEntry> loadResultsByClusterName(String clusterName, Timestamp interval_start_time, Integer limitRows) throws Exception;

// Load all recommendations of a particular experiment
List<KruizeRecommendationEntry> loadRecommendationsByExperimentName(String experimentName) throws Exception;
// Load all recommendations of a particular cluster
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/autotune/database/dao/ExperimentDAOImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,36 @@ public List<KruizeResultsEntry> loadResultsByExperimentName(String experimentNam
}
return kruizeResultsEntries;
}
@Override
public List<KruizeResultsEntry> loadResultsByClusterName(String clusterName, Timestamp interval_end_time, Integer limitRows) throws Exception {
// TODO: load only experimentStatus=inProgress , playback may not require completed experiments
List<KruizeResultsEntry> kruizeResultsEntries = null;
String statusValue = "failure";
Timer.Sample timerLoadResultsExpName = Timer.start(MetricsConfig.meterRegistry());
try (Session session = KruizeHibernateUtil.getSessionFactory().openSession()) {
if (null != limitRows && null != interval_end_time) {
kruizeResultsEntries = session.createQuery(DBConstants.SQLQUERY.SELECT_FROM_RESULTS_BY_CLUSTER_NAME_AND_DATE_RANGE, KruizeResultsEntry.class)
.setParameter(KruizeConstants.JSONKeys.CLUSTER_NAME, clusterName)
.setParameter(KruizeConstants.JSONKeys.INTERVAL_END_TIME, interval_end_time)
.setMaxResults(limitRows)
.list();
statusValue = "success";
} else {
kruizeResultsEntries = session.createQuery(DBConstants.SQLQUERY.SELECT_FROM_RESULTS_BY_CLUSTER_NAME, KruizeResultsEntry.class)
.setParameter("clsuterName", clusterName).list();
statusValue = "success";
}
} catch (Exception e) {
LOGGER.error("Not able to load results due to: {}", e.getMessage());
throw new Exception("Error while loading results from the database due to : " + e.getMessage());
} finally {
if (null != timerLoadResultsExpName) {
MetricsConfig.timerLoadResultsExpName = MetricsConfig.timerBLoadResultsExpName.tag("status", statusValue).register(MetricsConfig.meterRegistry());
timerLoadResultsExpName.stop(MetricsConfig.timerLoadResultsExpName);
}
}
return kruizeResultsEntries;
}

@Override
public List<KruizeRecommendationEntry> loadRecommendationsByExperimentName(String experimentName) throws Exception {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/autotune/database/helper/DBConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public static final class SQLQUERY {
public static final String SELECT_FROM_RESULTS_BY_EXP_NAME_AND_START_END_TIME = String.format("from KruizeResultsEntry k WHERE k.experiment_name = :%s and k.interval_start_time >= :%s and k.interval_end_time <= :%s", KruizeConstants.JSONKeys.EXPERIMENT_NAME, KruizeConstants.JSONKeys.INTERVAL_START_TIME, KruizeConstants.JSONKeys.INTERVAL_END_TIME);
public static final String SELECT_FROM_RESULTS_BY_EXP_NAME_AND_END_TIME = String.format("from KruizeResultsEntry k WHERE k.experiment_name = :%s and k.interval_end_time = :%s", KruizeConstants.JSONKeys.EXPERIMENT_NAME, KruizeConstants.JSONKeys.INTERVAL_END_TIME);
public static final String SELECT_FROM_RESULTS_BY_EXP_NAME_AND_MAX_END_TIME = String.format("from KruizeResultsEntry k WHERE k.experiment_name = :%s and k.interval_end_time = (SELECT MAX(e.interval_end_time) FROM KruizeResultsEntry e where e.experiment_name = :%s )", KruizeConstants.JSONKeys.EXPERIMENT_NAME, KruizeConstants.JSONKeys.EXPERIMENT_NAME);
public static final String SELECT_FROM_RESULTS_BY_CLUSTER_NAME = "from KruizeResultsEntry k WHERE k.cluster_name = :clusterName";
public static final String SELECT_FROM_RESULTS_BY_CLUSTER_NAME_AND_DATE_RANGE = String.format("from KruizeResultsEntry k WHERE k.cluster_name = :%s and k.interval_end_time <= :%s ORDER BY k.interval_end_time DESC", KruizeConstants.JSONKeys.CLUSTER_NAME, KruizeConstants.JSONKeys.INTERVAL_END_TIME, KruizeConstants.JSONKeys.INTERVAL_START_TIME);
public static final String SELECT_FROM_RECOMMENDATIONS_BY_EXP_NAME = "from KruizeRecommendationEntry k WHERE k.experiment_name = :experimentName";
public static final String SELECT_FROM_RECOMMENDATIONS_BY_EXP_NAME_AND_END_TIME = String.format("from KruizeRecommendationEntry k WHERE k.experiment_name = :%s and k.interval_end_time= :%s", KruizeConstants.JSONKeys.EXPERIMENT_NAME, KruizeConstants.JSONKeys.INTERVAL_END_TIME);
public static final String SELECT_FROM_RECOMMENDATIONS_BY_CLUSTER_NAME = "from KruizeRecommendationEntry k WHERE k.cluster_name = :clusterName";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,32 @@ public void loadRecommendationsFromDBByName(Map<String, KruizeObject> mainKruize
}
}
}
///
// Todo - confirm if experimentResultData to be changed to clusterResultData
public void loadResultsFromDBByClusterName(Map<String, KruizeObject> mainKruizeExperimentMap, String clusterName, Timestamp interval_end_time, Integer limitRows) throws Exception {
ExperimentInterface experimentInterface = new ExperimentInterfaceImpl();
// Load results from the DB and save to local
List<KruizeResultsEntry> kruizeResultsEntries = experimentDAO.loadResultsByClusterName(clusterName, interval_end_time, limitRows);
if (null != kruizeResultsEntries && !kruizeResultsEntries.isEmpty()) {
List<UpdateResultsAPIObject> updateResultsAPIObjects = DBHelpers.Converters.KruizeObjectConverters.convertResultEntryToUpdateResultsAPIObject(kruizeResultsEntries);
if (null != updateResultsAPIObjects && !updateResultsAPIObjects.isEmpty()) {
List<ExperimentResultData> resultDataList = new ArrayList<>();
for (UpdateResultsAPIObject updateResultsAPIObject : updateResultsAPIObjects) {
try {
ExperimentResultData experimentResultData = Converters.KruizeObjectConverters.convertUpdateResultsAPIObjToExperimentResultData(updateResultsAPIObject);
if (experimentResultData != null)
resultDataList.add(experimentResultData);
else
LOGGER.warn("Converted experimentResultData is null");
} catch (IllegalArgumentException e) {
LOGGER.error("Failed to convert DB data to local: {}", e.getMessage());
} catch (Exception e) {
LOGGER.error("Unexpected error: {}", e.getMessage());
}
}
experimentInterface.addResultsToLocalStorage(mainKruizeExperimentMap, resultDataList);
}
}
}
public void loadRecommendationsFromDBByClusterName(Map<String, KruizeObject> mainKruizeExperimentMap, String clusterName) throws Exception {
ExperimentInterface experimentInterface = new ExperimentInterfaceImpl();
// Load Recommendations from DB and save to local
Expand Down Expand Up @@ -362,6 +387,11 @@ public void loadExperimentAndRecommendationsFromDBByName(Map<String, KruizeObjec
loadRecommendationsFromDBByName(mainKruizeExperimentMap, experimentName);
}

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

loadExperimentsFromDBByClusterName(mainKruizeExperimentMap, clusterName);
loadResultsFromDBByClusterName(mainKruizeExperimentMap, clusterName, null, null);
}
public void loadExperimentsAndRecommendationsFromDBByClusterName(Map<String, KruizeObject> mainKruizeExperimentMap, String clusterName) throws Exception{
loadExperimentsFromDBByClusterName(mainKruizeExperimentMap, clusterName);
loadRecommendationsFromDBByClusterName(mainKruizeExperimentMap, clusterName);
Expand Down

0 comments on commit 2a4d524

Please sign in to comment.