Skip to content

Commit

Permalink
Merge pull request #1412 from shekhar316/adding-updater-abstraction
Browse files Browse the repository at this point in the history
3. Adding Recommendation Updater Abstraction and VPA Updater [KRUIZE-VPA Integration]
  • Loading branch information
dinogun authored Dec 13, 2024
2 parents 08f7a5e + 223b155 commit da8ae97
Show file tree
Hide file tree
Showing 9 changed files with 713 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*******************************************************************************
* 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.analyzer.exceptions;

public class ApplyRecommendationsError extends Exception {
public ApplyRecommendationsError() {
}

public ApplyRecommendationsError(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*******************************************************************************
* 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.analyzer.exceptions;

public class InvalidRecommendationUpdaterType extends Exception {
public InvalidRecommendationUpdaterType() {
}

public InvalidRecommendationUpdaterType(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*******************************************************************************
* 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.analyzer.exceptions;

public class UnableToCreateVPAException extends Exception {
public UnableToCreateVPAException() {
}

public UnableToCreateVPAException(String message) {
super(message);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*******************************************************************************
* 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.analyzer.recommendations.updater;

import com.autotune.analyzer.exceptions.ApplyRecommendationsError;
import com.autotune.analyzer.exceptions.InvalidRecommendationUpdaterType;
import com.autotune.analyzer.kruizeObject.KruizeObject;

/**
* This interface defines the abstraction for updating resource recommendations in a system.
* Implementing classes will provide the logic to update resources with recommendations for a specific resources,
* such as CPU, memory, or any other resources that require periodic or dynamic adjustments.
*
* The RecommendationUpdater interface is designed to be extended by different updater classes.
* For example, vpaUpdaterImpl for updating resources with recommendations related to CPU and memory resources.
*/

public interface RecommendationUpdater {
/**
* Retrieves an instance of a specific updater implementation based on the provided updater type
*
* @param updaterType String the type of updater to retrieve
* @return RecommendationUpdaterImpl An instance of provided updater type class
* @throws InvalidRecommendationUpdaterType If the provided updater type doesn't match any valid type of updater.
*/
RecommendationUpdaterImpl getUpdaterInstance(String updaterType) throws InvalidRecommendationUpdaterType;

/**
* Checks whether the necessary updater dependencies are installed or available in the system.
*
* @return boolean true if the required updaters are installed, false otherwise.
*/
boolean isUpdaterInstalled();

/**
* Generates resource recommendations for a specific experiment based on the experiment's name.
*
* @param experimentName String The name of the experiment for which the resource recommendations should be generated.
* @return KruizeObject containing recommendations
*/
KruizeObject generateResourceRecommendationsForExperiment(String experimentName);

/**
* Applies the resource recommendations contained within the provided KruizeObject
* This method will take the KruizeObject, which contains the resource recommendations,
* and apply them to the desired resources.
*
* @param kruizeObject KruizeObject containing the resource recommendations to be applied.
* @throws ApplyRecommendationsError in case of any error.
*/
void applyResourceRecommendationsForExperiment(KruizeObject kruizeObject) throws ApplyRecommendationsError;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*******************************************************************************
* 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.analyzer.recommendations.updater;

import com.autotune.analyzer.exceptions.ApplyRecommendationsError;
import com.autotune.analyzer.exceptions.FetchMetricsError;
import com.autotune.analyzer.exceptions.InvalidRecommendationUpdaterType;
import com.autotune.analyzer.kruizeObject.KruizeObject;
import com.autotune.analyzer.recommendations.engine.RecommendationEngine;
import com.autotune.analyzer.recommendations.updater.vpa.VpaUpdaterImpl;
import com.autotune.analyzer.utils.AnalyzerConstants;
import com.autotune.analyzer.utils.AnalyzerErrorConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RecommendationUpdaterImpl implements RecommendationUpdater {

private static final Logger LOGGER = LoggerFactory.getLogger(RecommendationUpdaterImpl.class);

/**
* Retrieves an instance of a specific updater implementation based on the provided updater type
*
* @param updaterType String the type of updater to retrieve
* @return RecommendationUpdaterImpl An instance of provided updater type class
* @throws InvalidRecommendationUpdaterType If the provided updater type doesn't match any valid type of updater.
*/
@Override
public RecommendationUpdaterImpl getUpdaterInstance(String updaterType) throws InvalidRecommendationUpdaterType {
if (AnalyzerConstants.RecommendationUpdaterConstants.SupportedUpdaters.VPA.equalsIgnoreCase(updaterType)) {
return VpaUpdaterImpl.getInstance();
} else {
throw new InvalidRecommendationUpdaterType(String.format(AnalyzerErrorConstants.RecommendationUpdaterErrors.UNSUPPORTED_UPDATER_TYPE, updaterType));
}
}

/**
* Checks whether the necessary updater dependencies are installed or available in the system.
* @return boolean true if the required updaters are installed, false otherwise.
*/
@Override
public boolean isUpdaterInstalled() {
/*
* This function will be implemented by specific updater type child classes
*/
return false;
}

/**
* Generates resource recommendations for a specific experiment based on the experiment's name.
*
* @param experimentName String The name of the experiment for which the resource recommendations should be generated.
* @return KruizeObject containing recommendations
*/
@Override
public KruizeObject generateResourceRecommendationsForExperiment(String experimentName) {
try {
LOGGER.debug(AnalyzerConstants.RecommendationUpdaterConstants.InfoMsgs.GENERATING_RECOMMENDATIONS, experimentName);
// generating latest recommendations for experiment
RecommendationEngine recommendationEngine = new RecommendationEngine(experimentName, null, null);
int calCount = 0;
String validationMessage = recommendationEngine.validate_local();
if (validationMessage.isEmpty()) {
KruizeObject kruizeObject = recommendationEngine.prepareRecommendations(calCount, null);
if (kruizeObject.getValidation_data().isSuccess()) {
LOGGER.debug(AnalyzerConstants.RecommendationUpdaterConstants.InfoMsgs.GENERATED_RECOMMENDATIONS, experimentName);
return kruizeObject;
} else {
throw new Exception(kruizeObject.getValidation_data().getMessage());
}
} else {
throw new Exception(validationMessage);
}
} catch (Exception | FetchMetricsError e) {
LOGGER.error(AnalyzerErrorConstants.RecommendationUpdaterErrors.GENERATE_RECOMMNEDATION_FAILED, experimentName);
LOGGER.debug(e.getMessage());
return null;
}
}

/**
* Applies the resource recommendations contained within the provided KruizeObject
* This method will take the KruizeObject, which contains the resource recommendations,
* and apply them to the desired resources.
*
* @param kruizeObject KruizeObject containing the resource recommendations to be applied.
* @throws ApplyRecommendationsError in case of any error.
*/
@Override
public void applyResourceRecommendationsForExperiment(KruizeObject kruizeObject) throws ApplyRecommendationsError {
/*
* This function will be implemented by specific updater type child classes
*/
}
}
Loading

0 comments on commit da8ae97

Please sign in to comment.