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

Fix the NPE issue in ListExperimentsAPI #1030

Merged
merged 3 commits into from
Jan 30, 2024
Merged
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
102 changes: 0 additions & 102 deletions src/main/java/com/autotune/analyzer/serviceObjects/Converters.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,108 +183,6 @@ public static ListRecommendationsAPIObject convertKruizeObjectToListRecommendati
return listRecommendationsAPIObject;
}

public static ListRecommendationsAPIObject convertKruizeObjectToListRecommendationSO(
KruizeObject kruizeObject,
boolean getLatest,
boolean checkForTimestamp,
String monitoringEndTimestamp) {
ListRecommendationsAPIObject listRecommendationsAPIObject = new ListRecommendationsAPIObject();
try {
listRecommendationsAPIObject.setApiVersion(AnalyzerConstants.VersionConstants.APIVersionConstants.CURRENT_LIST_RECOMMENDATIONS_VERSION);
listRecommendationsAPIObject.setExperimentName(kruizeObject.getExperimentName());
listRecommendationsAPIObject.setClusterName(kruizeObject.getClusterName());
List<KubernetesAPIObject> kubernetesAPIObjects = new ArrayList<>();
KubernetesAPIObject kubernetesAPIObject;
for (K8sObject k8sObject : kruizeObject.getKubernetes_objects()) {
kubernetesAPIObject = new KubernetesAPIObject(k8sObject.getName(), k8sObject.getType(), k8sObject.getNamespace());
HashMap<String, ContainerData> containerDataMap = new HashMap<>();
List<ContainerAPIObject> containerAPIObjects = new ArrayList<>();
for (ContainerData containerData : k8sObject.getContainerDataMap().values()) {
ContainerAPIObject containerAPIObject;
// if a Time stamp is passed it holds the priority than latest
if (checkForTimestamp) {
// This step causes a performance degradation, need to be replaced with a better flow of creating SO's
ContainerData clonedContainerData = Utils.getClone(containerData, ContainerData.class);
if (null != clonedContainerData) {
HashMap<Timestamp, MappedRecommendationForTimestamp> recommendations = clonedContainerData.getContainerRecommendations().getData();
Date medDate = Utils.DateUtils.getDateFrom(KruizeConstants.DateFormats.STANDARD_JSON_DATE_FORMAT, monitoringEndTimestamp);
Timestamp givenTimestamp = new Timestamp(medDate.getTime());
if (recommendations.containsKey(givenTimestamp)) {
List<Timestamp> tempList = new ArrayList<>();
for (Timestamp timestamp : recommendations.keySet()) {
if (!timestamp.equals(givenTimestamp))
tempList.add(timestamp);
}
for (Timestamp timestamp : tempList) {
recommendations.remove(timestamp);
}
clonedContainerData.getContainerRecommendations().setData(recommendations);
containerAPIObject = new ContainerAPIObject(clonedContainerData.getContainer_name(),
clonedContainerData.getContainer_image_name(),
clonedContainerData.getContainerRecommendations(),
new ArrayList<>(clonedContainerData.getMetrics().values()));
containerAPIObjects.add(containerAPIObject);
}
}
} else if (getLatest) {
// This step causes a performance degradation, need to be replaced with a better flow of creating SO's
containerData = getLatestRecommendations(containerData);
containerAPIObject = new ContainerAPIObject(containerData.getContainer_name(),
containerData.getContainer_image_name(),
containerData.getContainerRecommendations(),
new ArrayList<>(containerData.getMetrics().values()));
containerAPIObjects.add(containerAPIObject);
} else {
containerAPIObject = new ContainerAPIObject(containerData.getContainer_name(),
containerData.getContainer_image_name(),
containerData.getContainerRecommendations(),
new ArrayList<>(containerData.getMetrics().values()));
containerAPIObjects.add(containerAPIObject);
containerDataMap.put(containerData.getContainer_name(), containerData);
}
}
kubernetesAPIObject.setContainerAPIObjects(containerAPIObjects);
kubernetesAPIObjects.add(kubernetesAPIObject);
}
listRecommendationsAPIObject.setKubernetesObjects(kubernetesAPIObjects);
} catch (Exception e) {
e.printStackTrace();
}
return listRecommendationsAPIObject;
}


/**
* @param containerData
* @return
*/
public static ContainerData getLatestRecommendations(ContainerData containerData) {
ContainerData clonedContainerData = Utils.getClone(containerData, ContainerData.class);
if (null != clonedContainerData) {
HashMap<Timestamp, MappedRecommendationForTimestamp> recommendations = clonedContainerData.getContainerRecommendations().getData();
Timestamp latestTimestamp = null;
List<Timestamp> tempList = new ArrayList<>();
for (Timestamp timestamp : recommendations.keySet()) {
if (null == latestTimestamp) {
latestTimestamp = timestamp;
} else {
if (timestamp.after(latestTimestamp)) {
tempList.add(latestTimestamp);
latestTimestamp = timestamp;
} else {
tempList.add(timestamp);
}
}
}
for (Timestamp timestamp : tempList) {
recommendations.remove(timestamp);
}
clonedContainerData.getContainerRecommendations().setData(recommendations);
}
return clonedContainerData;
}


/**
* @param containerData
*/
Expand Down
51 changes: 2 additions & 49 deletions src/main/java/com/autotune/analyzer/services/ListExperiments.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -384,7 +385,7 @@ private void buildRecommendationsResponse(Map<String, KruizeObject> mKruizeExper
ko,
getLatest,
false,
(String) null);
null);

mergeRecommendationsInKruizeObject(listRecommendationsAPIObject, ko);
} catch (Exception e) {
Expand All @@ -397,54 +398,6 @@ private void mergeRecommendationsInKruizeObject(ListRecommendationsAPIObject lis
ko.setKubernetes_objects(convertKubernetesAPIObjectListToK8sObjectList(listRecommendationsAPIObject.getKubernetesObjects()));
}

//TODO this function no more used.
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType(JSON_CONTENT_TYPE);
response.setCharacterEncoding(CHARACTER_ENCODING);

LOGGER.info("Processing trial result...");
try {
String experimentName = request.getParameter(AnalyzerConstants.ServiceConstants.EXPERIMENT_NAME);
// String deploymentName = request.getParameter(AnalyzerConstants.ServiceConstants.DEPLOYMENT_NAME);

String trialResultsData = request.getReader().lines().collect(Collectors.joining());
JSONObject trialResultsJson = new JSONObject(trialResultsData);

// Read in the experiment name and the deployment name in the received JSON from EM
String experimentNameJson = trialResultsJson.getString(EXPERIMENT_NAME);
String trialNumber = trialResultsJson.getString("trialNumber");

JSONArray deploymentsJsonArray = trialResultsJson.getJSONArray("deployments");
for (Object deploymentObject : deploymentsJsonArray) {
JSONObject deploymentJsonObject = (JSONObject) deploymentObject;
String deploymentNameJson = deploymentJsonObject.getString(DEPLOYMENT_NAME);
KruizeExperiment kruizeExperiment = experimentsMap.get(deploymentNameJson);

// Check if the passed in JSON has the same info as in the URL
if (!experimentName.equals(experimentNameJson) || kruizeExperiment == null) {
LOGGER.error("Bad results JSON passed: {}", experimentNameJson);
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
break;
}

try {
updateExperimentTrial(trialNumber, kruizeExperiment, trialResultsJson);
} catch (InvalidValueException | IncompatibleInputJSONException e) {
e.printStackTrace();
}
RunExperiment runExperiment = kruizeExperiment.getExperimentThread();
// Received a metrics JSON from EM after a trial, let the waiting thread know
LOGGER.info("Received trial result for experiment: " + experimentNameJson + "; Deployment name: " + deploymentNameJson);
runExperiment.send();
}
response.getWriter().close();
} catch (Exception e) {
e.printStackTrace();
}
}

public void sendErrorResponse(HttpServletResponse response, Exception e, int httpStatusCode, String errorMsg) throws
IOException {
if (null != e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,19 @@ public void loadAllExperiments(Map<String, KruizeObject> mainKruizeExperimentMap

public void loadAllResults(Map<String, KruizeObject> mainKruizeExperimentMap) throws Exception {
ExperimentInterface experimentInterface = new ExperimentInterfaceImpl();

KruizeObject kruizeObject;
// Load results from the DB and save to local
List<KruizeResultsEntry> kruizeResultsEntries = experimentDAO.loadAllResults();
if (null != kruizeResultsEntries && !kruizeResultsEntries.isEmpty()) {
List<UpdateResultsAPIObject> updateResultsAPIObjects = DBHelpers.Converters.KruizeObjectConverters.convertResultEntryToUpdateResultsAPIObject(kruizeResultsEntries);
if (null != updateResultsAPIObjects && !updateResultsAPIObjects.isEmpty()) {
if (!updateResultsAPIObjects.isEmpty()) {
List<ExperimentResultData> resultDataList = new ArrayList<>();
for (UpdateResultsAPIObject updateResultsAPIObject : updateResultsAPIObjects) {
try {
kruizeObject = mainKruizeExperimentMap.get(updateResultsAPIObject.getExperimentName());
updateResultsAPIObject.setKruizeObject(kruizeObject);
ExperimentResultData experimentResultData = Converters.KruizeObjectConverters.convertUpdateResultsAPIObjToExperimentResultData(updateResultsAPIObject);
if (experimentResultData != null)
resultDataList.add(experimentResultData);
else
LOGGER.warn("Converted experimentResultData is null");
resultDataList.add(experimentResultData);
} catch (IllegalArgumentException e) {
LOGGER.error("Failed to convert DB data to local: {}", e.getMessage());
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ echo ""
echo "Running fault tolerant test for kruize on ${CLUSTER_TYPE}" | tee -a ${LOG}
if [ "${CLUSTER_TYPE}" == "openshift" ]; then
echo "python3 kruize_pod_restart_test.py -c ${CLUSTER_TYPE} -a ${SERVER_IP_ADDR} -u ${num_exps} -d ${iterations} -r ${LOG_DIR} | tee -a ${TEST_LOG}" | tee -a ${LOG}
python3 kruize_pod_restart_test.py -c ${CLUSTER_TYPE} -a ${SERVER_IP_ADDR} -u ${num_exps} -d ${iterations} -r "${LOG_DIR}"
python3 kruize_pod_restart_test.py -c ${CLUSTER_TYPE} -a ${SERVER_IP_ADDR} -u ${num_exps} -d ${iterations} -r "${LOG_DIR}" | tee -a ${TEST_LOG}
exit_code=$?
echo "exit_code = $exit_code"

else
echo "python3 kruize_pod_restart_test.py -c ${CLUSTER_TYPE} -u ${num_exps} -d ${iterations} -r ${LOG_DIR} | tee -a ${TEST_LOG}" | tee -a ${LOG}
python3 kruize_pod_restart_test.py -c ${CLUSTER_TYPE} -u ${num_exps} -d ${iterations} -r "${LOG_DIR}"
python3 kruize_pod_restart_test.py -c ${CLUSTER_TYPE} -u ${num_exps} -d ${iterations} -r "${LOG_DIR}" | tee -a ${TEST_LOG}
exit_code=$?
echo "exit_code = $exit_code"
fi
Expand Down
Loading