Skip to content

Commit 890a1e3

Browse files
authored
Merge pull request #62 from comet-ml/CM-2345-delete-registry-model
[CM-2345]: Implement equivalent of API.delete_registry_model
2 parents bac421f + 86fd01f commit 890a1e3

File tree

8 files changed

+74
-1
lines changed

8 files changed

+74
-1
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ 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 the registry model
207+
//
208+
System.out.printf("Deleting registry model '%s/%s'.\n", experiment.getWorkspaceName(), newModelName);
209+
api.deleteRegistryModel(newModelName, experiment.getWorkspaceName());
210+
System.out.println("Model was successfully deleted.");
211+
206212
} finally {
207213
PathUtils.deleteDirectory(modelTmpDir);
208214
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,12 @@ void updateRegistryModelVersion(String registryName, String workspace, String ve
157157
String comments, List<String> stages);
158158

159159
void updateRegistryModelVersion(String registryName, String workspace, String version, String comments);
160+
161+
/**
162+
* Deletes registered model with given name.
163+
*
164+
* @param registryName the name of the model.
165+
* @param workspace the name of the model's workspace.
166+
*/
167+
void deleteRegistryModel(String registryName, String workspace);
160168
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import static ml.comet.experiment.impl.resources.LogMessages.DOWNLOADING_REGISTRY_MODEL_TO_FILE;
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;
70+
import static ml.comet.experiment.impl.resources.LogMessages.FAILED_TO_DELETE_REGISTRY_MODEL;
7071
import static ml.comet.experiment.impl.resources.LogMessages.FAILED_TO_DOWNLOAD_REGISTRY_MODEL;
7172
import static ml.comet.experiment.impl.resources.LogMessages.FAILED_TO_FIND_EXPERIMENT_MODEL_BY_NAME;
7273
import static ml.comet.experiment.impl.resources.LogMessages.FAILED_TO_GET_REGISTRY_MODEL_DETAILS;
@@ -424,6 +425,13 @@ public void updateRegistryModelVersion(String registryName, String workspace, St
424425
this.updateRegistryModelVersion(registryName, workspace, version, null, null);
425426
}
426427

428+
@Override
429+
public void deleteRegistryModel(String registryName, String workspace) {
430+
RestApiResponse response = this.restApiClient.deleteRegistryModel(registryName, workspace)
431+
.blockingGet();
432+
this.checkRestApiResponse(response, getString(FAILED_TO_DELETE_REGISTRY_MODEL, registryName, workspace));
433+
}
434+
427435
/**
428436
* Release all resources hold by this instance, such as connection to the Comet server.
429437
*

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
import static ml.comet.experiment.impl.constants.ApiEndpoints.ADD_TAG;
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;
80+
import static ml.comet.experiment.impl.constants.ApiEndpoints.DELETE_REGISTRY_MODEL;
8081
import static ml.comet.experiment.impl.constants.ApiEndpoints.DOWNLOAD_REGISTRY_MODEL;
8182
import static ml.comet.experiment.impl.constants.ApiEndpoints.EXPERIMENTS;
8283
import static ml.comet.experiment.impl.constants.ApiEndpoints.GET_ARTIFACT_VERSION_DETAIL;
@@ -115,6 +116,8 @@
115116
import static ml.comet.experiment.impl.constants.QueryParamName.TYPE;
116117
import static ml.comet.experiment.impl.constants.QueryParamName.WORKSPACE_NAME;
117118
import static ml.comet.experiment.impl.http.ConnectionUtils.checkResponseStatus;
119+
import static ml.comet.experiment.impl.resources.LogMessages.NO_RESPONSE_RETURNED_BY_REMOTE_ENDPOINT;
120+
import static ml.comet.experiment.impl.resources.LogMessages.getString;
118121
import static ml.comet.experiment.impl.utils.RestApiUtils.artifactDownloadAssetParams;
119122
import static ml.comet.experiment.impl.utils.RestApiUtils.artifactVersionDetailsParams;
120123
import static ml.comet.experiment.impl.utils.RestApiUtils.artifactVersionFilesParams;
@@ -385,6 +388,13 @@ Single<RestApiResponse> updateRegistryModelVersion(RegistryModelUpdateItemReques
385388
return singleFromAsyncPost(request, UPDATE_REGISTRY_MODEL_VERSION);
386389
}
387390

391+
Single<RestApiResponse> deleteRegistryModel(String modelName, String workspaceName) {
392+
Map<QueryParamName, String> queryParams = new HashMap<>();
393+
queryParams.put(WORKSPACE_NAME, workspaceName);
394+
queryParams.put(MODEL_NAME, modelName);
395+
return singleFromSyncGetWithRetries(DELETE_REGISTRY_MODEL, queryParams);
396+
}
397+
388398
private Single<RestApiResponse> singleFromAsyncDownload(@NonNull OutputStream output,
389399
@NonNull String endpoint,
390400
@NonNull Map<QueryParamName, String> queryParams) {
@@ -489,7 +499,15 @@ private Single<RestApiResponse> singleFromSyncPostWithRetriesEmptyBody(@NonNull
489499
return this.connection.sendPostWithRetries(JsonUtils.toJson(payload), endpoint, true)
490500
.map(body -> Single.just(new RestApiResponse(200, body)))
491501
.orElse(Single.error(new CometApiException(
492-
String.format("No response was returned by endpoint: %s", endpoint))));
502+
getString(NO_RESPONSE_RETURNED_BY_REMOTE_ENDPOINT, endpoint))));
503+
}
504+
505+
private Single<RestApiResponse> singleFromSyncGetWithRetries(@NonNull String endpoint,
506+
@NonNull Map<QueryParamName, String> params) {
507+
return this.connection.sendGetWithRetries(endpoint, params, true)
508+
.map(body -> Single.just(new RestApiResponse(200, body)))
509+
.orElse(Single.error(new CometApiException(
510+
getString(NO_RESPONSE_RETURNED_BY_REMOTE_ENDPOINT, endpoint))));
493511
}
494512

495513
private <T> Single<T> singleFromSyncGetWithRetries(@NonNull String endpoint,

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
@@ -28,6 +28,7 @@ public final class ApiEndpoints {
2828
public static final String UPDATE_REGISTRY_MODEL_NOTES = UPDATE_API_URL + "/registry-model/notes";
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";
31+
public static final String DELETE_REGISTRY_MODEL = UPDATE_API_URL + "/registry-model/delete";
3132

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

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ public class LogMessages {
9797
public static final String FAILED_TO_UPDATE_REGISTRY_MODEL = "FAILED_TO_UPDATE_REGISTRY_MODEL";
9898
public static final String FAILED_TO_UPDATE_REGISTRY_MODEL_VERSION = "FAILED_TO_UPDATE_REGISTRY_MODEL_VERSION";
9999
public static final String REGISTRY_MODEL_VERSION_NOT_FOUND = "REGISTRY_MODEL_VERSION_NOT_FOUND";
100+
public static final String FAILED_TO_DELETE_REGISTRY_MODEL = "FAILED_TO_DELETE_REGISTRY_MODEL";
101+
public static final String NO_RESPONSE_RETURNED_BY_REMOTE_ENDPOINT = "NO_RESPONSE_RETURNED_BY_REMOTE_ENDPOINT";
100102

101103

102104
/**

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,5 @@ FAILED_TO_UPDATE_REGISTRY_MODEL_NOTES=Failed to update notes of the registry mod
8282
FAILED_TO_UPDATE_REGISTRY_MODEL=Failed to update registry model '%s/%s' with data: '%s'.
8383
FAILED_TO_UPDATE_REGISTRY_MODEL_VERSION=Failed to update registry model's version '%s/%s:%s' with data: '%s'.
8484
REGISTRY_MODEL_VERSION_NOT_FOUND=Version '%s' of the registry model '%s/%s' is not found.
85+
FAILED_TO_DELETE_REGISTRY_MODEL=Failed to delete registry model '%s/%s'.
86+
NO_RESPONSE_RETURNED_BY_REMOTE_ENDPOINT=No response was returned by endpoint '%s'

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,34 @@ public void testUpdateRegistryModelVersion() {
550550
assertEquals(stages, versionOverview.getStages(), "wrong stages");
551551
}
552552

553+
@Test
554+
public void testDeleteRegistryModel() {
555+
String modelName = String.format("%s-%d", SOME_MODEL_NAME, System.currentTimeMillis());
556+
557+
// register model with defaults
558+
//
559+
registerModelWithDefaults(modelName);
560+
561+
// wait for registry model to be processed by backend
562+
//
563+
Awaitility.await("failed to get registry model")
564+
.pollInterval(1, TimeUnit.SECONDS)
565+
.atMost(60, TimeUnit.SECONDS)
566+
.until(() -> COMET_API.getRegistryModelDetails(modelName,
567+
SHARED_EXPERIMENT.getWorkspaceName()).isPresent());
568+
569+
// try to delete the model
570+
//
571+
COMET_API.deleteRegistryModel(modelName, SHARED_EXPERIMENT.getWorkspaceName());
572+
}
573+
574+
@Test
575+
public void testDeleteRegistryModel_doesnt_exists() {
576+
String modelName = "not existing model";
577+
assertThrows(CometApiException.class, () ->
578+
COMET_API.deleteRegistryModel(modelName, SHARED_EXPERIMENT.getWorkspaceName()));
579+
}
580+
553581
private static String registerModelWithDefaults(String modelName) {
554582
// log model folder
555583
//

0 commit comments

Comments
 (0)