|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.core.ml.action; |
| 9 | + |
| 10 | +import org.elasticsearch.action.ActionRequestValidationException; |
| 11 | +import org.elasticsearch.action.ActionType; |
| 12 | +import org.elasticsearch.action.support.master.AcknowledgedRequest; |
| 13 | +import org.elasticsearch.action.support.master.AcknowledgedResponse; |
| 14 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 15 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 16 | +import org.elasticsearch.xpack.core.ml.inference.TrainedModelConfig; |
| 17 | +import org.elasticsearch.xpack.core.ml.job.messages.Messages; |
| 18 | +import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.Locale; |
| 22 | +import java.util.Objects; |
| 23 | +import java.util.regex.Pattern; |
| 24 | + |
| 25 | +import static org.elasticsearch.action.ValidateActions.addValidationError; |
| 26 | +import static org.elasticsearch.xpack.core.ml.job.messages.Messages.INVALID_MODEL_ALIAS; |
| 27 | + |
| 28 | +public class PutTrainedModelAliasAction extends ActionType<AcknowledgedResponse> { |
| 29 | + |
| 30 | + // NOTE this is similar to our valid ID check. The difference here is that model_aliases cannot end in numbers |
| 31 | + // This is to protect our automatic model naming conventions from hitting weird model_alias conflicts |
| 32 | + private static final Pattern VALID_MODEL_ALIAS_CHAR_PATTERN = Pattern.compile("[a-z0-9](?:[a-z0-9_\\-\\.]*[a-z])?"); |
| 33 | + |
| 34 | + public static final PutTrainedModelAliasAction INSTANCE = new PutTrainedModelAliasAction(); |
| 35 | + public static final String NAME = "cluster:admin/xpack/ml/inference/model_aliases/put"; |
| 36 | + |
| 37 | + private PutTrainedModelAliasAction() { |
| 38 | + super(NAME, AcknowledgedResponse::readFrom); |
| 39 | + } |
| 40 | + |
| 41 | + public static class Request extends AcknowledgedRequest<Request> { |
| 42 | + |
| 43 | + public static final String MODEL_ALIAS = "model_alias"; |
| 44 | + public static final String REASSIGN = "reassign"; |
| 45 | + |
| 46 | + private final String modelAlias; |
| 47 | + private final String modelId; |
| 48 | + private final boolean reassign; |
| 49 | + |
| 50 | + public Request(String modelAlias, String modelId, boolean reassign) { |
| 51 | + this.modelAlias = ExceptionsHelper.requireNonNull(modelAlias, MODEL_ALIAS); |
| 52 | + this.modelId = ExceptionsHelper.requireNonNull(modelId, TrainedModelConfig.MODEL_ID); |
| 53 | + this.reassign = reassign; |
| 54 | + } |
| 55 | + |
| 56 | + public Request(StreamInput in) throws IOException { |
| 57 | + super(in); |
| 58 | + this.modelAlias = in.readString(); |
| 59 | + this.modelId = in.readString(); |
| 60 | + this.reassign = in.readBoolean(); |
| 61 | + } |
| 62 | + |
| 63 | + public String getModelAlias() { |
| 64 | + return modelAlias; |
| 65 | + } |
| 66 | + |
| 67 | + public String getModelId() { |
| 68 | + return modelId; |
| 69 | + } |
| 70 | + |
| 71 | + public boolean isReassign() { |
| 72 | + return reassign; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void writeTo(StreamOutput out) throws IOException { |
| 77 | + super.writeTo(out); |
| 78 | + out.writeString(modelAlias); |
| 79 | + out.writeString(modelId); |
| 80 | + out.writeBoolean(reassign); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public ActionRequestValidationException validate() { |
| 85 | + ActionRequestValidationException validationException = null; |
| 86 | + if (modelAlias.equals(modelId)) { |
| 87 | + validationException = addValidationError( |
| 88 | + String.format( |
| 89 | + Locale.ROOT, |
| 90 | + "model_alias [%s] cannot equal model_id [%s]", |
| 91 | + modelAlias, |
| 92 | + modelId |
| 93 | + ), |
| 94 | + validationException |
| 95 | + ); |
| 96 | + } |
| 97 | + if (VALID_MODEL_ALIAS_CHAR_PATTERN.matcher(modelAlias).matches() == false) { |
| 98 | + validationException = addValidationError(Messages.getMessage(INVALID_MODEL_ALIAS, modelAlias), validationException); |
| 99 | + } |
| 100 | + return validationException; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public boolean equals(Object o) { |
| 105 | + if (this == o) return true; |
| 106 | + if (o == null || getClass() != o.getClass()) return false; |
| 107 | + Request request = (Request) o; |
| 108 | + return Objects.equals(modelAlias, request.modelAlias) |
| 109 | + && Objects.equals(modelId, request.modelId) |
| 110 | + && Objects.equals(reassign, request.reassign); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public int hashCode() { |
| 115 | + return Objects.hash(modelAlias, modelId, reassign); |
| 116 | + } |
| 117 | + |
| 118 | + } |
| 119 | +} |
0 commit comments