Skip to content

Commit

Permalink
update datasource validation to check with DB first
Browse files Browse the repository at this point in the history
Signed-off-by: Saad Khan <[email protected]>
  • Loading branch information
khansaad committed Sep 18, 2024
1 parent 1063af8 commit e798581
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -469,16 +469,3 @@ spec:
- name: nginx-config-volume
configMap:
name: nginx-config
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: kruize-monitoring-view
subjects:
- kind: ServiceAccount
name: kruize-sa
namespace: openshift-tuning
roleRef:
kind: ClusterRole
name: cluster-monitoring-view
apiGroup: rbac.authorization.k8s.io
Original file line number Diff line number Diff line change
Expand Up @@ -1772,9 +1772,15 @@ private String getResults(Map<String, KruizeObject> mainKruizeExperimentMAP, Kru
}
} else if (kruizeObject.getExperiment_usecase_type().isLocal_monitoring()) {
// get data from the provided datasource in case of local monitoring
DataSourceInfo dataSourceInfo = DataSourceCollection.getInstance().getDataSourcesCollection().get(dataSource);
if (dataSourceInfo == null) {
throw new DataSourceNotExist(KruizeConstants.DataSourceConstants.DataSourceErrorMsgs.MISSING_DATASOURCE_INFO);
// fetch the DatasourceInfo object from the DB based on datasource name
DataSourceInfo dataSourceInfo = new ExperimentDBService().loadDataSourceFromDBByName(dataSource);
// If the DB result is invalid, check the DataSourceCollection
if (CommonUtils.isInvalidDataSource(dataSourceInfo)) {
dataSourceInfo = DataSourceCollection.getInstance().getDataSourcesCollection().get(dataSource);
// If DataSourceCollection is also invalid, return with error
if (CommonUtils.isInvalidDataSource(dataSourceInfo)) {
throw new DataSourceNotExist(KruizeConstants.DataSourceConstants.DataSourceErrorMsgs.INVALID_DATASOURCE_INFO);
}
}
// Fetch metrics dynamically from Metric Profile based on the datasource
fetchMetricsBasedOnProfileAndDatasource(kruizeObject, interval_end_time, intervalStartTime, dataSourceInfo);
Expand Down
29 changes: 17 additions & 12 deletions src/main/java/com/autotune/analyzer/services/DSMetadataService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.autotune.common.datasource.DataSourceInfo;
import com.autotune.common.datasource.DataSourceManager;
import com.autotune.common.datasource.DataSourceMetadataValidation;
import com.autotune.common.utils.CommonUtils;
import com.autotune.database.service.ExperimentDBService;
import com.autotune.utils.KruizeConstants;
import com.autotune.utils.KruizeSupportedTypes;
Expand Down Expand Up @@ -115,18 +116,22 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
if (validationOutputData.isSuccess()) {

String dataSourceName = metadataAPIObject.getDataSourceName();
// fetch the DatasourceInfo object from the DataSourceCollection based on datasource name
DataSourceInfo datasource = DataSourceCollection.getInstance().getDataSourcesCollection().get(dataSourceName);

if (datasource == null) {
sendErrorResponse(
inputData,
response,
new Exception(AnalyzerErrorConstants.APIErrors.DSMetadataAPI.INVALID_DATASOURCE_NAME_METADATA_EXCPTN),
HttpServletResponse.SC_BAD_REQUEST,
String.format(AnalyzerErrorConstants.APIErrors.DSMetadataAPI.DATASOURCE_METADATA_IMPORT_ERROR_MSG, dataSourceName)
);
return;
// fetch the DatasourceInfo object from the DB based on datasource name
DataSourceInfo datasource = dataSourceManager.fetchDataSourceFromDBByName(dataSourceName);
// If the DB result is invalid, check the DataSourceCollection
if (CommonUtils.isInvalidDataSource(datasource)) {
datasource = DataSourceCollection.getInstance().getDataSourcesCollection().get(dataSourceName);
// If DataSourceCollection is also invalid, return with error
if (CommonUtils.isInvalidDataSource(datasource)) {
sendErrorResponse(
inputData,
response,
new Exception(AnalyzerErrorConstants.APIErrors.DSMetadataAPI.INVALID_DATASOURCE_NAME_METADATA_EXCPTN),
HttpServletResponse.SC_BAD_REQUEST,
String.format(AnalyzerErrorConstants.APIErrors.DSMetadataAPI.DATASOURCE_METADATA_IMPORT_ERROR_MSG, dataSourceName)
);
return;
}
}

DataSourceMetadataInfo metadataInfo = dataSourceManager.importMetadataFromDataSource(datasource);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/autotune/common/utils/CommonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,4 +293,10 @@ public static double getPercentage(double newer, double older) {

return ((newer - older)/older) * 100;
}

// Helper method to validate the DataSourceInfo object
public static boolean isInvalidDataSource(DataSourceInfo datasource) {
return datasource == null || datasource.getAuthenticationConfig() == null ||
datasource.getAuthenticationConfig().toString().isEmpty();
}
}
1 change: 1 addition & 0 deletions src/main/java/com/autotune/utils/KruizeConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ public static class DataSourceErrorMsgs {
public static final String SERVICE_NOT_FOUND = "Can not find service with specified name.";
public static final String ENDPOINT_NOT_FOUND = "Service endpoint not found.";
public static final String MISSING_DATASOURCE_INFO = "Datasource is missing, add a valid Datasource";
public static final String INVALID_DATASOURCE_INFO = "Datasource is either missing or is invalid";
private DataSourceErrorMsgs() {
}
}
Expand Down

0 comments on commit e798581

Please sign in to comment.