-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Bhavana Ramaram <rbhavna@amazon.com>
- Loading branch information
Showing
9 changed files
with
307 additions
and
31 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
...rc/main/java/org/opensearch/ml/common/transport/model_group/MLModelGroupDeleteAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.transport.model_group; | ||
|
||
import org.opensearch.action.ActionType; | ||
import org.opensearch.action.delete.DeleteResponse; | ||
|
||
public class MLModelGroupDeleteAction extends ActionType<DeleteResponse> { | ||
public static final MLModelGroupDeleteAction INSTANCE = new MLModelGroupDeleteAction(); | ||
public static final String NAME = "cluster:admin/opensearch/ml/model_groups/delete"; | ||
|
||
private MLModelGroupDeleteAction() { super(NAME, DeleteResponse::new);} | ||
} |
70 changes: 70 additions & 0 deletions
70
...c/main/java/org/opensearch/ml/common/transport/model_group/MLModelGroupDeleteRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.common.transport.model_group; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import org.opensearch.action.ActionRequest; | ||
import org.opensearch.action.ActionRequestValidationException; | ||
import org.opensearch.common.io.stream.InputStreamStreamInput; | ||
import org.opensearch.common.io.stream.OutputStreamStreamOutput; | ||
import org.opensearch.common.io.stream.StreamInput; | ||
import org.opensearch.common.io.stream.StreamOutput; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
|
||
import static org.opensearch.action.ValidateActions.addValidationError; | ||
|
||
public class MLModelGroupDeleteRequest extends ActionRequest { | ||
@Getter | ||
String modelGroupId; | ||
|
||
@Builder | ||
public MLModelGroupDeleteRequest(String modelGroupId) { | ||
this.modelGroupId = modelGroupId; | ||
} | ||
|
||
public MLModelGroupDeleteRequest(StreamInput input) throws IOException { | ||
super(input); | ||
this.modelGroupId = input.readString(); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput output) throws IOException { | ||
super.writeTo(output); | ||
output.writeString(modelGroupId); | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
ActionRequestValidationException exception = null; | ||
|
||
if (this.modelGroupId == null) { | ||
exception = addValidationError("ML model group id can't be null", exception); | ||
} | ||
|
||
return exception; | ||
} | ||
|
||
public static MLModelGroupDeleteRequest fromActionRequest(ActionRequest actionRequest) { | ||
if (actionRequest instanceof MLModelGroupDeleteRequest) { | ||
return (MLModelGroupDeleteRequest)actionRequest; | ||
} | ||
|
||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
OutputStreamStreamOutput osso = new OutputStreamStreamOutput(baos)) { | ||
actionRequest.writeTo(osso); | ||
try (StreamInput input = new InputStreamStreamInput(new ByteArrayInputStream(baos.toByteArray()))) { | ||
return new MLModelGroupDeleteRequest(input); | ||
} | ||
} catch (IOException e) { | ||
throw new UncheckedIOException("failed to parse ActionRequest into MLModelGroupDeleteRequest", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
...n/src/main/java/org/opensearch/ml/action/model_group/DeleteModelGroupTransportAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.ml.action.model_group; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.experimental.FieldDefaults; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.opensearch.action.ActionListener; | ||
import org.opensearch.action.ActionRequest; | ||
import org.opensearch.action.delete.DeleteRequest; | ||
import org.opensearch.action.delete.DeleteResponse; | ||
import org.opensearch.action.search.SearchRequest; | ||
import org.opensearch.action.support.ActionFilters; | ||
import org.opensearch.action.support.HandledTransportAction; | ||
import org.opensearch.client.Client; | ||
import org.opensearch.cluster.service.ClusterService; | ||
import org.opensearch.common.inject.Inject; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.common.util.concurrent.ThreadContext; | ||
import org.opensearch.commons.authuser.User; | ||
import org.opensearch.core.xcontent.NamedXContentRegistry; | ||
import org.opensearch.index.query.BoolQueryBuilder; | ||
import org.opensearch.index.query.TermQueryBuilder; | ||
import org.opensearch.ml.common.exception.MLValidationException; | ||
import org.opensearch.ml.common.transport.model_group.MLModelGroupDeleteAction; | ||
import org.opensearch.ml.common.transport.model_group.MLModelGroupDeleteRequest; | ||
import org.opensearch.ml.utils.RestActionUtils; | ||
import org.opensearch.ml.utils.SecurityUtils; | ||
import org.opensearch.search.builder.SearchSourceBuilder; | ||
import org.opensearch.tasks.Task; | ||
import org.opensearch.transport.TransportService; | ||
|
||
import static org.opensearch.ml.common.CommonValue.ML_MODEL_GROUP_INDEX; | ||
import static org.opensearch.ml.common.CommonValue.ML_MODEL_INDEX; | ||
import static org.opensearch.ml.settings.MLCommonsSettings.ML_COMMONS_VALIDATE_BACKEND_ROLES; | ||
import static org.opensearch.ml.utils.RestActionUtils.PARAMETER_MODEL_GROUP_ID; | ||
|
||
@Log4j2 | ||
@FieldDefaults(level = AccessLevel.PRIVATE) | ||
public class DeleteModelGroupTransportAction extends HandledTransportAction<ActionRequest, DeleteResponse> { | ||
|
||
Client client; | ||
NamedXContentRegistry xContentRegistry; | ||
ClusterService clusterService; | ||
|
||
private volatile boolean filterByEnabled; | ||
|
||
@Inject | ||
public DeleteModelGroupTransportAction( | ||
TransportService transportService, | ||
ActionFilters actionFilters, | ||
Client client, | ||
NamedXContentRegistry xContentRegistry, | ||
Settings settings, | ||
ClusterService clusterService | ||
) { | ||
super(MLModelGroupDeleteAction.NAME, transportService, actionFilters, MLModelGroupDeleteRequest::new); | ||
this.client = client; | ||
this.xContentRegistry = xContentRegistry; | ||
this.clusterService = clusterService; | ||
filterByEnabled = ML_COMMONS_VALIDATE_BACKEND_ROLES.get(settings); | ||
clusterService.getClusterSettings().addSettingsUpdateConsumer(ML_COMMONS_VALIDATE_BACKEND_ROLES, it -> filterByEnabled = it); | ||
} | ||
|
||
@Override | ||
protected void doExecute(Task task, ActionRequest request, ActionListener<DeleteResponse> actionListener) { | ||
MLModelGroupDeleteRequest mlModelGroupDeleteRequest = MLModelGroupDeleteRequest.fromActionRequest(request); | ||
String modelGroupId = mlModelGroupDeleteRequest.getModelGroupId(); | ||
DeleteRequest deleteRequest = new DeleteRequest(ML_MODEL_GROUP_INDEX, modelGroupId); | ||
User user = RestActionUtils.getUserContext(client); | ||
|
||
try (ThreadContext.StoredContext context = client.threadPool().getThreadContext().stashContext()) { | ||
SecurityUtils.validateModelGroupAccess(user, modelGroupId, client, ActionListener.wrap(access -> { | ||
if ((filterByEnabled) && (!access)) { | ||
actionListener | ||
.onFailure(new MLValidationException("User Doesn't have previlege to perform this operation")); | ||
} else { | ||
BoolQueryBuilder query = new BoolQueryBuilder(); | ||
query.filter(new TermQueryBuilder(PARAMETER_MODEL_GROUP_ID, modelGroupId)); | ||
log.info(query.toString()); | ||
|
||
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(query); | ||
SearchRequest searchRequest = new SearchRequest(ML_MODEL_INDEX).source(searchSourceBuilder); | ||
client.search(searchRequest, ActionListener.wrap(mlModels -> { | ||
if (mlModels == null || mlModels.getHits().getTotalHits() == null || mlModels.getHits().getTotalHits().value == 0) { | ||
client.delete(deleteRequest, new ActionListener<DeleteResponse>() { | ||
@Override | ||
public void onResponse(DeleteResponse deleteResponse) { | ||
log.debug("Completed Delete Model Group Request, task id:{} deleted", modelGroupId); | ||
actionListener.onResponse(deleteResponse); | ||
} | ||
|
||
@Override | ||
public void onFailure(Exception e) { | ||
log.error("Failed to delete ML Model Group " + modelGroupId, e); | ||
actionListener.onFailure(e); | ||
} | ||
}); | ||
} else { | ||
throw new MLValidationException("Cannot delete the model group when it has associated model versions"); | ||
} | ||
|
||
}, e -> { | ||
log.error("Failed to search models with the specified Model Group Id " + modelGroupId, e); | ||
actionListener.onFailure(e); | ||
})); | ||
} | ||
}, e -> { | ||
log.error("Failed to validate Access for Model Group " + modelGroupId, e); | ||
actionListener.onFailure(e); | ||
})); | ||
} catch (Exception e) { | ||
log.error("Failed to delete ml model group" + modelGroupId, e); | ||
actionListener.onFailure(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.