Skip to content

Commit e46c37c

Browse files
authored
Merge pull request #63 from comet-ml/CM-2346-delete-registry-model-version
[CM-2346]: Implement equivalent of API.delete_registry_model_version
2 parents 890a1e3 + 094ce9a commit e46c37c

File tree

9 files changed

+107
-18
lines changed

9 files changed

+107
-18
lines changed

comet-examples/src/main/java/ml/comet/examples/RegistryModelExample.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,13 @@ record = api.registerModel(updatedModel, experiment.getExperimentKey());
203203
newComment, newStages);
204204
System.out.printf("Model version details was successfully updated for: %s\n", SOME_MODEL_VERSION_UP);
205205

206+
// delete registry model version
207+
//
208+
System.out.printf("Deleting registry model version '%s/%s:%s'.\n",
209+
experiment.getWorkspaceName(), newModelName, SOME_MODEL_VERSION);
210+
api.deleteRegistryModelVersion(newModelName, experiment.getWorkspaceName(), SOME_MODEL_VERSION);
211+
System.out.printf("Version '%s' of the registry model was successfully deleted.\n", SOME_MODEL_VERSION);
212+
206213
// delete the registry model
207214
//
208215
System.out.printf("Deleting registry model '%s/%s'.\n", experiment.getWorkspaceName(), newModelName);

comet-java-client/src/main/java/ml/comet/experiment/CometApi.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,13 @@ void updateRegistryModelVersion(String registryName, String workspace, String ve
165165
* @param workspace the name of the model's workspace.
166166
*/
167167
void deleteRegistryModel(String registryName, String workspace);
168+
169+
/**
170+
* Deletes specific version of the registered model with given name.
171+
*
172+
* @param registryName the name of the model.
173+
* @param workspace the name of the model's workspace.
174+
* @param version the version of the registered model to be deleted.
175+
*/
176+
void deleteRegistryModelVersion(String registryName, String workspace, String version);
168177
}

comet-java-client/src/main/java/ml/comet/experiment/impl/CometApiImpl.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import static ml.comet.experiment.impl.resources.LogMessages.EXPERIMENT_HAS_NO_MODELS;
6969
import static ml.comet.experiment.impl.resources.LogMessages.EXTRACTED_N_REGISTRY_MODEL_FILES;
7070
import static ml.comet.experiment.impl.resources.LogMessages.FAILED_TO_DELETE_REGISTRY_MODEL;
71+
import static ml.comet.experiment.impl.resources.LogMessages.FAILED_TO_DELETE_REGISTRY_MODEL_VERSION;
7172
import static ml.comet.experiment.impl.resources.LogMessages.FAILED_TO_DOWNLOAD_REGISTRY_MODEL;
7273
import static ml.comet.experiment.impl.resources.LogMessages.FAILED_TO_FIND_EXPERIMENT_MODEL_BY_NAME;
7374
import static ml.comet.experiment.impl.resources.LogMessages.FAILED_TO_GET_REGISTRY_MODEL_DETAILS;
@@ -388,22 +389,9 @@ private void updateRegistryModel(@NonNull String registryName, @NonNull String w
388389
@Override
389390
public void updateRegistryModelVersion(@NonNull String registryName, @NonNull String workspace,
390391
@NonNull String version, String comments, List<String> stages) {
391-
// get registry model details
392-
Optional<ModelOverview> overviewOptional = this.getRegistryModelDetails(registryName, workspace);
393-
if (!overviewOptional.isPresent()) {
394-
throw new ModelNotFoundException(getString(REGISTRY_MODEL_NOT_FOUND, workspace, registryName));
395-
}
396-
397-
ModelOverview modelOverview = overviewOptional.get();
398-
if (modelOverview.getVersions() == null || modelOverview.getVersions().size() == 0) {
399-
throw new ModelNotFoundException(getString(FAILED_TO_GET_REGISTRY_MODEL_VERSIONS, workspace, registryName));
400-
}
401-
402-
// get registry model's item ID which match version
403-
Optional<ModelVersionOverview> versionOverviewOptional = modelOverview.getVersions()
404-
.stream()
405-
.filter(modelVersionOverview -> Objects.equals(modelVersionOverview.getVersion(), version))
406-
.findFirst();
392+
// get version details
393+
Optional<ModelVersionOverview> versionOverviewOptional = this.getRegistryModelVersion(
394+
registryName, workspace, version);
407395
if (!versionOverviewOptional.isPresent()) {
408396
throw new ModelVersionNotFoundException(
409397
getString(REGISTRY_MODEL_VERSION_NOT_FOUND, version, workspace, registryName));
@@ -426,12 +414,28 @@ public void updateRegistryModelVersion(String registryName, String workspace, St
426414
}
427415

428416
@Override
429-
public void deleteRegistryModel(String registryName, String workspace) {
417+
public void deleteRegistryModel(@NonNull String registryName, @NonNull String workspace) {
430418
RestApiResponse response = this.restApiClient.deleteRegistryModel(registryName, workspace)
431419
.blockingGet();
432420
this.checkRestApiResponse(response, getString(FAILED_TO_DELETE_REGISTRY_MODEL, registryName, workspace));
433421
}
434422

423+
@Override
424+
public void deleteRegistryModelVersion(@NonNull String registryName, @NonNull String workspace,
425+
@NonNull String version) {
426+
// get version details
427+
Optional<ModelVersionOverview> versionOverviewOptional = this.getRegistryModelVersion(
428+
registryName, workspace, version);
429+
if (!versionOverviewOptional.isPresent()) {
430+
throw new ModelVersionNotFoundException(
431+
getString(REGISTRY_MODEL_VERSION_NOT_FOUND, version, workspace, registryName));
432+
}
433+
String errorMsg = getString(FAILED_TO_DELETE_REGISTRY_MODEL_VERSION, workspace, registryName, version);
434+
RestApiResponse response = this.executeSyncRequest(this.restApiClient::deleteRegistryModelVersion,
435+
versionOverviewOptional.get().getRegistryModelItemId(), errorMsg);
436+
this.checkRestApiResponse(response, errorMsg);
437+
}
438+
435439
/**
436440
* Release all resources hold by this instance, such as connection to the Comet server.
437441
*

comet-java-client/src/main/java/ml/comet/experiment/impl/RestApiClient.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
import static ml.comet.experiment.impl.constants.ApiEndpoints.CREATE_REGISTRY_MODEL;
7979
import static ml.comet.experiment.impl.constants.ApiEndpoints.CREATE_REGISTRY_MODEL_ITEM;
8080
import static ml.comet.experiment.impl.constants.ApiEndpoints.DELETE_REGISTRY_MODEL;
81+
import static ml.comet.experiment.impl.constants.ApiEndpoints.DELETE_REGISTRY_MODEL_ITEM;
8182
import static ml.comet.experiment.impl.constants.ApiEndpoints.DOWNLOAD_REGISTRY_MODEL;
8283
import static ml.comet.experiment.impl.constants.ApiEndpoints.EXPERIMENTS;
8384
import static ml.comet.experiment.impl.constants.ApiEndpoints.GET_ARTIFACT_VERSION_DETAIL;
@@ -111,6 +112,7 @@
111112
import static ml.comet.experiment.impl.constants.QueryParamName.ARTIFACT_VERSION_ID;
112113
import static ml.comet.experiment.impl.constants.QueryParamName.EXPERIMENT_KEY;
113114
import static ml.comet.experiment.impl.constants.QueryParamName.IS_REMOTE;
115+
import static ml.comet.experiment.impl.constants.QueryParamName.MODEL_ITEM_ID;
114116
import static ml.comet.experiment.impl.constants.QueryParamName.MODEL_NAME;
115117
import static ml.comet.experiment.impl.constants.QueryParamName.PROJECT_ID;
116118
import static ml.comet.experiment.impl.constants.QueryParamName.TYPE;
@@ -395,6 +397,12 @@ Single<RestApiResponse> deleteRegistryModel(String modelName, String workspaceNa
395397
return singleFromSyncGetWithRetries(DELETE_REGISTRY_MODEL, queryParams);
396398
}
397399

400+
Single<RestApiResponse> deleteRegistryModelVersion(String modelItemId) {
401+
Map<QueryParamName, String> queryParams = new HashMap<>();
402+
queryParams.put(MODEL_ITEM_ID, modelItemId);
403+
return singleFromSyncGetWithRetries(DELETE_REGISTRY_MODEL_ITEM, queryParams);
404+
}
405+
398406
private Single<RestApiResponse> singleFromAsyncDownload(@NonNull OutputStream output,
399407
@NonNull String endpoint,
400408
@NonNull Map<QueryParamName, String> queryParams) {

comet-java-client/src/main/java/ml/comet/experiment/impl/constants/ApiEndpoints.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public final class ApiEndpoints {
2929
public static final String UPDATE_REGISTRY_MODEL = UPDATE_API_URL + "/registry-model/update";
3030
public static final String UPDATE_REGISTRY_MODEL_VERSION = UPDATE_API_URL + "/registry-model/item/update";
3131
public static final String DELETE_REGISTRY_MODEL = UPDATE_API_URL + "/registry-model/delete";
32+
public static final String DELETE_REGISTRY_MODEL_ITEM = UPDATE_API_URL + "/registry-model/item/delete";
3233

3334
public static final String READ_API_URL = "/api/rest/v2";
3435
public static final String GET_ASSETS_LIST = READ_API_URL + "/experiment/asset/list";

comet-java-client/src/main/java/ml/comet/experiment/impl/constants/QueryParamName.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ public enum QueryParamName {
3434
ASSET_ID("assetId"), // string
3535

3636
MODEL_NAME("modelName"), // string
37-
STAGE("stage"); // string
37+
STAGE("stage"), // string
38+
39+
MODEL_ITEM_ID("modelItemId"); // string
3840

3941

4042
private final String paramName;

comet-java-client/src/main/java/ml/comet/experiment/impl/resources/LogMessages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public class LogMessages {
9999
public static final String REGISTRY_MODEL_VERSION_NOT_FOUND = "REGISTRY_MODEL_VERSION_NOT_FOUND";
100100
public static final String FAILED_TO_DELETE_REGISTRY_MODEL = "FAILED_TO_DELETE_REGISTRY_MODEL";
101101
public static final String NO_RESPONSE_RETURNED_BY_REMOTE_ENDPOINT = "NO_RESPONSE_RETURNED_BY_REMOTE_ENDPOINT";
102+
public static final String FAILED_TO_DELETE_REGISTRY_MODEL_VERSION = "FAILED_TO_DELETE_REGISTRY_MODEL_VERSION";
102103

103104

104105
/**

comet-java-client/src/main/resources/messages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,4 @@ FAILED_TO_UPDATE_REGISTRY_MODEL_VERSION=Failed to update registry model's versio
8484
REGISTRY_MODEL_VERSION_NOT_FOUND=Version '%s' of the registry model '%s/%s' is not found.
8585
FAILED_TO_DELETE_REGISTRY_MODEL=Failed to delete registry model '%s/%s'.
8686
NO_RESPONSE_RETURNED_BY_REMOTE_ENDPOINT=No response was returned by endpoint '%s'
87+
FAILED_TO_DELETE_REGISTRY_MODEL_VERSION=Failed to delete registry model '%s/%s:%s'.

comet-java-client/src/test/java/ml/comet/experiment/impl/CometApiTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,62 @@ private static String registerTwoModelVersions(String modelName, String nextVers
633633
return model.getRegistryName();
634634
}
635635

636+
@Test
637+
public void testDeleteRegistryModelVersion_model_doesnt_exists() {
638+
// try to delete version of not existing model
639+
//
640+
String modelName = "not existing model";
641+
assertThrows(CometApiException.class, ()->
642+
COMET_API.deleteRegistryModelVersion(modelName, SHARED_EXPERIMENT.getWorkspaceName(),
643+
DEFAULT_MODEL_VERSION));
644+
}
645+
646+
@Test
647+
public void testDeleteRegistryModelVersion_version_doesnt_exists() {
648+
String modelName = String.format("%s-%d", SOME_MODEL_NAME, System.currentTimeMillis());
649+
650+
// register model with defaults
651+
//
652+
registerModelWithDefaults(modelName);
653+
654+
// try to delete not existing version of the model
655+
//
656+
assertThrows(ModelVersionNotFoundException.class, ()->
657+
COMET_API.deleteRegistryModelVersion(modelName, SHARED_EXPERIMENT.getWorkspaceName(),
658+
"1.0.1"));
659+
}
660+
661+
@Test
662+
public void testDeleteRegistryModelVersion() {
663+
// Create model with two versions and check
664+
//
665+
String modelName = String.format("%s-%d", SOME_MODEL_NAME, System.currentTimeMillis());
666+
String newVersion = "1.0.1";
667+
String newComment = "updated model";
668+
List<String> stages = Collections.singletonList("production");
669+
registerTwoModelVersions(modelName, newVersion, newComment, stages);
670+
671+
// wait for model versions to be processed by backend
672+
//
673+
Awaitility.await("failed to get registry model version")
674+
.pollInterval(1, TimeUnit.SECONDS)
675+
.atMost(60, TimeUnit.SECONDS)
676+
.until(() -> COMET_API.getRegistryModelVersion(
677+
modelName, SHARED_EXPERIMENT.getWorkspaceName(), newVersion).isPresent());
678+
679+
// Delete model version
680+
//
681+
COMET_API.deleteRegistryModelVersion(modelName, SHARED_EXPERIMENT.getWorkspaceName(), DEFAULT_MODEL_VERSION);
682+
683+
// Check that version was deleted
684+
//
685+
Awaitility.await("registry model version was not deleted")
686+
.pollInterval(1, TimeUnit.SECONDS)
687+
.atMost(60, TimeUnit.SECONDS)
688+
.until(() -> !COMET_API.getRegistryModelVersion(
689+
modelName, SHARED_EXPERIMENT.getWorkspaceName(), DEFAULT_MODEL_VERSION).isPresent());
690+
}
691+
636692
// UnZip model's file into temporary directory and check that expected model files are present
637693
private static void checkModelZipFile(Path zipFilePath, String... fileNames) throws IOException {
638694
Path tmpDir = Files.createTempDirectory("checkModelZipFile");

0 commit comments

Comments
 (0)